Fix 10649: False positive: wrong known value after reassignment (#3631)

This commit is contained in:
Paul Fultz II 2021-12-15 12:37:25 -06:00 committed by GitHub
parent dafb2fe6a0
commit 5414814238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -4578,6 +4578,17 @@ static ValueFlow::Value makeSymbolic(const Token* tok, MathLib::bigint delta = 0
return value;
}
static std::set<nonneg int> getVarIds(const Token* tok)
{
std::set<nonneg int> result;
visitAstNodes(tok, [&](const Token* child) {
if (child->varId() > 0)
result.insert(child->varId());
return ChildrenToVisit::op1_and_op2;
});
return result;
}
static void valueFlowSymbolic(TokenList* tokenlist, SymbolDatabase* symboldatabase)
{
for (const Scope* scope : symboldatabase->functionScopes) {
@ -4607,8 +4618,11 @@ static void valueFlowSymbolic(TokenList* tokenlist, SymbolDatabase* symboldataba
} else if (isDifferentType(tok->astOperand2(), tok->astOperand1())) {
continue;
}
const std::set<nonneg int> rhsVarIds = getVarIds(tok->astOperand2());
const std::vector<const Variable*> vars = getLHSVariables(tok);
if (std::any_of(vars.begin(), vars.end(), [](const Variable* var) {
if (std::any_of(vars.begin(), vars.end(), [&](const Variable* var) {
if (rhsVarIds.count(var->declarationId()) > 0)
return true;
if (var->isLocal())
return var->isStatic();
return !var->isArgument();

View File

@ -3937,6 +3937,17 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #10649
check("void foo(struct diag_msg *msg) {\n"
" msg = msg->next;\n"
" if (msg == NULL)\n"
" return CMD_OK;\n"
" msg = msg->next;\n"
" if (msg == NULL)\n"
" return CMD_OK;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void alwaysTrueInfer() {

View File

@ -2092,7 +2092,8 @@ private:
" if (!y) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS(
TODO_ASSERT_EQUALS(
"",
"[test.cpp:13] -> [test.cpp:9]: (warning) Either the condition '!y' is redundant or there is possible null pointer dereference: x->g().\n",
errout.str());
}