diff --git a/CheckOther.cpp b/CheckOther.cpp index 2985a7294..6a2e0dadf 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -325,7 +325,11 @@ void CheckUnsignedDivision() for ( TOKEN *tok = tokens; tok; tok = tok->next ) { if ( Match(tok, "[{};(,] %type% %var% [;=,)]") ) - varsign[getstr(tok,2)] = 's'; + { + const char *type = getstr(tok, 1); + if (strcmp(type,"char")==0 || strcmp(type,"short")==0 || strcmp(type,"int")==0) + varsign[getstr(tok,2)] = 's'; + } else if ( Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") ) varsign[getstr(tok,3)] = 'u'; @@ -634,7 +638,7 @@ void CheckCharVariable() else if ( tok2->str[0] == '}' ) { --indentlevel; - if ( indentlevel < 0 ) + if ( indentlevel <= 0 ) break; } diff --git a/main.cpp b/main.cpp index c733f916f..0f269525a 100644 --- a/main.cpp +++ b/main.cpp @@ -260,10 +260,9 @@ static void CppCheck(const char FileName[], unsigned int FileId) CheckMemset(); - // Check for unwanted unsigned division - // Not accurate yet. Very important to run it before 'SimplifyTokenList' - if ( ShowAll ) - CheckUnsignedDivision(); + // Check for unsigned divisions where one operand is signed + // Very important to run it before 'SimplifyTokenList' + CheckUnsignedDivision(); // Give warning when using char variable as array index // Doesn't work on simplified token list ('unsigned') diff --git a/testdivision.cpp b/testdivision.cpp index abf954082..be275361e 100644 --- a/testdivision.cpp +++ b/testdivision.cpp @@ -38,6 +38,7 @@ public: TEST_CASE( division2 ); TEST_CASE( division3 ); TEST_CASE( division4 ); + TEST_CASE( division5 ); } void division1() @@ -89,6 +90,17 @@ public: ); ASSERT_EQUALS( std::string(""), errout.str() ); } + + void division5() + { + check( "#define USER_HASH (16)\n" + "void foo()\n" + "{\n" + " unsigned int val = 32;\n" + " val = val / USER_HASH;\n" + ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } }; REGISTER_FIXTURE( TestDivision )