checkZeroDivisionOrUselessCondition: Fixed false positive when function is called and variable is passed to it

This commit is contained in:
Daniel Marjamäki 2013-09-07 16:18:11 +02:00
parent 4973ae5183
commit 6bb6506ce2
2 changed files with 27 additions and 12 deletions

View File

@ -2177,24 +2177,32 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
break;
}
}
bool ifcondition = false;
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
if (tok2->varId() == varid)
break;
if (tok2->str() == "}")
break;
if (Token::Match(tok2, "%var% (") && tok2->str() != "if" && (var->isGlobal() || !tok2->function()))
break;
if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 0 < %varid% &&|%oror%|)", varid)) {
zerodivcondError(tok2,tok);
continue;
if (Token::Match(tok2, "%var% (")) {
if (Token::simpleMatch(tok2, "if ("))
ifcondition = true;
else if (var->isGlobal() || !tok2->function())
break;
}
if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 1 <= %varid% &&|%oror%|)", varid)) {
zerodivcondError(tok2,tok);
continue;
}
if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) {
zerodivcondError(tok2,tok);
continue;
if (ifcondition) {
if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 0 < %varid% &&|%oror%|)", varid)) {
zerodivcondError(tok2,tok);
continue;
}
if (isVarUnsigned && Token::Match(tok2, "(|%oror%|&& 1 <= %varid% &&|%oror%|)", varid)) {
zerodivcondError(tok2,tok);
continue;
}
if (Token::Match(tok2, "(|%oror%|&& !| %varid% &&|%oror%|)", varid)) {
zerodivcondError(tok2,tok);
continue;
}
}
}
}

View File

@ -486,6 +486,13 @@ private:
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'x!=0' is useless or there is division by zero at line 4.\n", errout.str());
}
check("void do_something(int value);\n"
"void f(int x) {\n"
" int y = 17 / x;\n"
" do_something(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int x;\n"
"void f() {\n"
" int y = 17 / x;\n"