FP on assignment through pointer (#1887)

* Fix FP when assigning through pointers

* Add test case for false positive

cppcheck would faulty warn:
"Condition '*b>0' is always true"
This commit is contained in:
Ken-Patrick 2019-06-17 21:25:15 +02:00 committed by Daniel Marjamäki
parent 246576fceb
commit 44d6066c6f
2 changed files with 17 additions and 1 deletions

View File

@ -1429,7 +1429,7 @@ FwdAnalysis::Result FwdAnalysis::check(const Token *expr, const Token *startToke
Result result = checkRecursive(expr, startToken, endToken, exprVarIds, local);
// Break => continue checking in outer scope
while (result.type == FwdAnalysis::Result::Type::BREAK) {
while (mWhat!=What::ValueFlow && result.type == FwdAnalysis::Result::Type::BREAK) {
const Scope *s = result.token->scope();
while (s->type == Scope::eIf)
s = s->nestedIn;

View File

@ -2566,6 +2566,22 @@ private:
"}";
values = tokenValues(code, "+");
TODO_ASSERT_EQUALS(2U, 0U, values.size()); // should be 2
// FP: Condition '*b>0' is always true
code = "bool dostuff(const char *x, const char *y);\n"
"void fun(char *s, int *b) {\n"
" for (int i = 0; i < 42; ++i) {\n"
" if (dostuff(s, \"1\")) {\n"
" *b = 1;\n"
" break;\n"
" }\n"
" }\n"
" if (*b > 0) {\n" // *b does not have known value
" }\n"
"}";
values = tokenValues(code, ">");
ASSERT_EQUALS(true, values.empty());
}
void valueFlowSwitchVariable() {