From e06b2419a48120e34474ad4e6c8d0e207257abd7 Mon Sep 17 00:00:00 2001 From: seb777 Date: Fri, 27 May 2011 02:04:47 +0800 Subject: [PATCH] fix #2786 (new check: Using sizeof with numeric constant) Add unit test and improve check with negative constant because in cppcheck %num% means 'integer' --- lib/checkother.cpp | 17 ++++++++++++++++- test/testother.cpp | 13 +++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4b0416786..801eaa513 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -214,7 +214,11 @@ void CheckOther::checkSizeofForNumericParameter() { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (Token::Match(tok, "sizeof ( %num% )") || Token::Match(tok, "sizeof %num%")) + if (Token::Match(tok, "sizeof ( %num% )") + || Token::Match(tok, "sizeof ( - %num% )") + || Token::Match(tok, "sizeof %num%") + || Token::Match(tok, "sizeof - %num%") + ) { sizeofForNumericParameterError(tok); } @@ -960,6 +964,17 @@ void CheckOther::checkUnsignedDivision() } } } + else if (Token::Match(tok, "|[|=|return|%op% %var% / %var%")) + { + + //std::cout << "cicicicic" << std::endl; + char sign1 = varsign[tok->tokAt(1)->varId()]; + char sign2 = varsign[tok->tokAt(3)->varId()]; + if ((sign1 == 'u' && sign2 == 's') || (sign1 == 's' && sign2 == 'u')) + { + //udivError(tok); + } + } } } diff --git a/test/testother.cpp b/test/testother.cpp index 06ab52065..ebea0b4c6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2313,12 +2313,25 @@ private: ); ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str()); + check("void f() {\n" + " std::cout << sizeof(-10) << std::endl;\n" + "}\n" + ); + ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str()); + check("void f() {\n" " std::cout << sizeof 10 << std::endl;\n" "}\n" ); ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str()); + check("void f() {\n" + " std::cout << sizeof -10 << std::endl;\n" + "}\n" + ); + ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str()); + + } void clarifyCalculation()