Skip to content

Commit e418933

Browse files
authored
Fix #15259 (isAliasOf: alias values are filtered again for every AST node in the expression) (#9062)
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.
1 parent 02148a4 commit e418933

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎lib/astutils.cpp‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,27 +1090,29 @@ bool isAliasOf(const Token* tok, const Token* expr, nonneg int* indirect)
10901090
const Token* r = nullptr;
10911091
for (const ReferenceToken& ref : tok->refs()) {
10921092
const bool pointer = astIsPointer(ref.token);
1093+
const bool deref = expr && ref.token != tok && ref.token->isUnaryOp("*") && expr->exprId() == ref.token->astOperand1()->exprId();
1094+
std::vector<const Token*> aliasValues;
1095+
for (const ValueFlow::Value& val : ref.token->values()) {
1096+
if (val.isImpossible())
1097+
continue;
1098+
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0))
1099+
aliasValues.push_back(val.tokvalue);
1100+
}
1101+
if (!deref && aliasValues.empty())
1102+
continue;
10931103
r = findAstNode(expr, [&](const Token* childTok) {
10941104
if (childTok->exprId() == 0)
10951105
return false;
1096-
if (ref.token != tok && expr->exprId() == childTok->exprId() && ref.token->isUnaryOp("*") && expr->exprId() == ref.token->astOperand1()->exprId()) {
1106+
if (deref && expr->exprId() == childTok->exprId()) {
10971107
if (indirect)
10981108
*indirect = 0;
10991109
return true;
11001110
}
1101-
for (const ValueFlow::Value& val : ref.token->values()) {
1102-
if (val.isImpossible())
1103-
continue;
1104-
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) {
1105-
if (findAstNode(val.tokvalue,
1106-
[&](const Token* aliasTok) {
1107-
return aliasTok != childTok && aliasTok->exprId() == childTok->exprId();
1108-
})) {
1109-
return true;
1110-
}
1111-
}
1112-
}
1113-
return false;
1111+
return std::any_of(aliasValues.cbegin(), aliasValues.cend(), [&](const Token* aliasValue) {
1112+
return findAstNode(aliasValue, [&](const Token* aliasTok) {
1113+
return aliasTok != childTok && aliasTok->exprId() == childTok->exprId();
1114+
}) != nullptr;
1115+
});
11141116
});
11151117
if (r)
11161118
break;

‎test/testvalueflow.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8484,6 +8484,20 @@ class TestValueFlow : public TestFixture {
84848484
void valueFlowCrash() {
84858485
const char* code;
84868486

8487+
code = "struct N {\n" // isAliasOf() was called with expr=nullptr
8488+
" N* r;\n"
8489+
" N root() const;\n"
8490+
" N f(const char* path, char d) const;\n"
8491+
"};\n"
8492+
"N N::f(const char* path, char d) const {\n"
8493+
" N c = path[0] == d ? root() : *this;\n"
8494+
" if (!c.r) return N();\n"
8495+
" const char* p = path;\n"
8496+
" while (*p == d) ++p;\n"
8497+
" return c;\n"
8498+
"}\n";
8499+
(void)valueOfTok(code, "p");
8500+
84878501
code = "void f(int x) {\n"
84888502
" if (0 * (x > 2)) {}\n"
84898503
"}\n";

0 commit comments

Comments
 (0)