ValueFlow: skip values that cause false assertion condition
This commit is contained in:
parent
fd85b493bd
commit
72e4bc9d88
|
@ -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) {
|
else if (tok2->str() == "}" && indentlevel == varusagelevel) {
|
||||||
++number_of_if;
|
++number_of_if;
|
||||||
|
|
||||||
|
|
|
@ -1796,6 +1796,20 @@ private:
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(false, testValueOfX(code, 6U, 5));
|
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
|
// hang
|
||||||
code = "void f() {\n"
|
code = "void f() {\n"
|
||||||
" for(int i = 0; i < 20; i++)\n"
|
" for(int i = 0; i < 20; i++)\n"
|
||||||
|
|
Loading…
Reference in New Issue