Skip to content

Commit b52fa94

Browse files
committed
Fix cppcheck-opensource#1385 (False positive: unsigned division)
http://sourceforge.net/apps/trac/cppcheck/ticket/1385 This also fixes a bug in setVarId(). "unsigned int a" didn't get varid, untill later when unsigned was simplified away.
1 parent 669fe1b commit b52fa94

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

lib/checkother.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,10 @@ void CheckOther::checkUnsignedDivision()
323323
else if (Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]"))
324324
varsign[tok->tokAt(3)->varId()] = 'u';
325325

326-
else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / %var%"))
326+
else if (!Token::Match(tok, "[).]") &&
327+
Token::Match(tok->next(), "%var% / %var%") &&
328+
tok->tokAt(1)->varId() != 0 &&
329+
tok->tokAt(3)->varId() != 0)
327330
{
328331
if (ErrorLogger::udivWarning(*_settings))
329332
{

lib/tokenize.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,9 +1830,12 @@ void Tokenizer::setVarId()
18301830
if (Token::Match(tok, "[,;{}(] %type%"))
18311831
tok = tok->next();
18321832

1833-
if (tok->str() == "new" || tok->str() == "unsigned")
1833+
if (tok->str() == "new")
18341834
continue;
18351835

1836+
if (tok->str() == "unsigned")
1837+
tok = tok->next();
1838+
18361839
if (Token::Match(tok, "class|struct %type% :|{|;"))
18371840
continue;
18381841

test/testdivision.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ class TestDivision : public TestFixture
115115
" result = i2 / i1;}\n"
116116
);
117117
ASSERT_EQUALS("", errout.str());
118+
119+
check("void f1()\n"
120+
"{\n"
121+
" unsigned int num = 0;\n"
122+
"}\n"
123+
"\n"
124+
"void f2(int X)\n"
125+
"{\n"
126+
" X = X / z;}\n"
127+
);
128+
ASSERT_EQUALS("", errout.str());
118129
}
119130

120131
void division5()

0 commit comments

Comments
 (0)