ValueFlow: skip values that cause false assertion condition

This commit is contained in:
Daniel Marjamäki 2016-12-17 21:23:14 +01:00
parent fd85b493bd
commit 72e4bc9d88
2 changed files with 27 additions and 0 deletions

View File

@ -1463,6 +1463,19 @@ static bool valueFlowForward(Token * const startToken,
}
}
else if (Token::Match(tok2, "assert|ASSERT (") && Token::simpleMatch(tok2->linkAt(1), ") ;")) {
const Token * const arg = tok2->next()->astOperand2();
if (arg != nullptr && arg->str() != ",") {
// Should scope be skipped because variable value is checked?
for (std::list<ValueFlow::Value>::const_iterator it = values.begin(); it != values.end();) {
if (conditionIsFalse(arg, getProgramMemory(tok2, varid, *it)))
values.erase(it++);
else
++it;
}
}
}
else if (tok2->str() == "}" && indentlevel == varusagelevel) {
++number_of_if;

View File

@ -1796,6 +1796,20 @@ private:
"}";
ASSERT_EQUALS(false, testValueOfX(code, 6U, 5));
// assert after for loop..
code = "static void f() {\n"
" int x;\n"
" int ctls[10];\n"
" for (x = 0; x <= 10; x++) {\n"
" if (cond)\n"
" break;\n"
" }\n"
" assert(x <= 10);\n"
" ctls[x] = 123;\n" // <- x can't be 11
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 9U, 11));
// hang
code = "void f() {\n"
" for(int i = 0; i < 20; i++)\n"