diff --git a/lib/astutils.cpp b/lib/astutils.cpp index a08d86a2f86..8495df36673 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1088,29 +1088,32 @@ bool isAliasOf(const Token* tok, const Token* expr, nonneg int* indirect) if (!tok) return false; const Token* r = nullptr; + std::vector aliasValues; for (const ReferenceToken& ref : tok->refs()) { const bool pointer = astIsPointer(ref.token); + const bool deref = expr && ref.token != tok && ref.token->isUnaryOp("*") && expr->exprId() == ref.token->astOperand1()->exprId(); + aliasValues.clear(); + for (const ValueFlow::Value& val : ref.token->values()) { + if (val.isImpossible()) + continue; + if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) + aliasValues.push_back(val.tokvalue); + } + if (!deref && aliasValues.empty()) + continue; r = findAstNode(expr, [&](const Token* childTok) { if (childTok->exprId() == 0) return false; - if (ref.token != tok && expr->exprId() == childTok->exprId() && ref.token->isUnaryOp("*") && expr->exprId() == ref.token->astOperand1()->exprId()) { + if (deref && expr->exprId() == childTok->exprId()) { if (indirect) *indirect = 0; return true; } - for (const ValueFlow::Value& val : ref.token->values()) { - if (val.isImpossible()) - continue; - if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) { - if (findAstNode(val.tokvalue, - [&](const Token* aliasTok) { - return aliasTok != childTok && aliasTok->exprId() == childTok->exprId(); - })) { - return true; - } - } - } - return false; + return std::any_of(aliasValues.cbegin(), aliasValues.cend(), [&](const Token* aliasValue) { + return findAstNode(aliasValue, [&](const Token* aliasTok) { + return aliasTok != childTok && aliasTok->exprId() == childTok->exprId(); + }) != nullptr; + }); }); if (r) break; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index dfbb98ad72c..d98ab0665f0 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -8357,6 +8357,20 @@ class TestValueFlow : public TestFixture { void valueFlowCrash() { const char* code; + code = "struct N {\n" // isAliasOf() was called with expr=nullptr + " N* r;\n" + " N root() const;\n" + " N f(const char* path, char d) const;\n" + "};\n" + "N N::f(const char* path, char d) const {\n" + " N c = path[0] == d ? root() : *this;\n" + " if (!c.r) return N();\n" + " const char* p = path;\n" + " while (*p == d) ++p;\n" + " return c;\n" + "}\n"; + (void)valueOfTok(code, "p"); + code = "void f(int x) {\n" " if (0 * (x > 2)) {}\n" "}\n";