diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index b5974c915..d6e88144c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2171,9 +2171,13 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol const std::size_t functions = symboldatabase->functionScopes.size(); for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symboldatabase->functionScopes[i]; + std::set aliased; for (Token* tok = const_cast(scope->classStart); tok != scope->classEnd; tok = tok->next()) { const Token *vartok, *numtok; + if (Token::Match(tok, "= & %var% ;")) + aliased.insert(tok->tokAt(2)->varId()); + // Comparison if (Token::Match(tok, "==|!=|>=|<=")) { if (!tok->astOperand1() || !tok->astOperand2()) @@ -2211,6 +2215,11 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol const Variable *var = vartok->variable(); if (!var || !(var->isLocal() || var->isGlobal() || var->isArgument())) continue; + if (aliased.find(varid) != aliased.end()) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, vartok, "variable is aliased so we just skip all valueflow after condition"); + continue; + } std::list values; values.push_back(ValueFlow::Value(tok, numtok ? numtok->values().front().intvalue : 0LL)); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 7209c360a..0add92f2e 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1743,6 +1743,17 @@ private: " a = x;\n" "}"; ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); + + // aliased variable + code = "void f() {\n" + " int x = 1;\n" + " int *data = &x;\n" + " if (!x) {\n" + " calc(data);\n" + " a = x;\n" // <- x might be changed by calc + " }\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 6U, 0)); } void valueFlowForwardCompoundAssign() {