From 9d1c87a636a041c84ef1f7b760c5662b4b1e3faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Nov 2026 09:03:24 +0100 Subject: [PATCH] Fix #15259 (isAliasOf: alias values are filtered again for every AST node in the expression) The values of the reference token were filtered again for every node in the expression. Filter them once and skip the AST search when there is nothing that can match. No changed warnings in a 50 package debian corpus, ValueFlow ~10% faster. Co-Authored-By: Claude Opus 5.5 --- lib/astutils.cpp | 31 +++++++++++++++++-------------- test/testvalueflow.cpp | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) 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";