Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Token*> 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;
Expand Down
14 changes: 14 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading