Skip to content

Commit 04ecbba

Browse files
committed
Fixed danmar#7135 (ValueFlow: Wrong pointer alias set for 'p = &p[x];')
1 parent 6cf7ff4 commit 04ecbba

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/valueflow.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,16 @@ static void valueFlowPointerAlias(TokenList *tokenlist)
703703
continue;
704704

705705
// child should be some buffer or variable
706-
if (!Token::Match(tok->astOperand1(), "%name%|.|[|;"))
706+
const Token *vartok = tok->astOperand1();
707+
while (vartok) {
708+
if (vartok->str() == "[")
709+
vartok = vartok->astOperand1();
710+
else if (vartok->str() == "." || vartok->str() == "::")
711+
vartok = vartok->astOperand2();
712+
else
713+
break;
714+
}
715+
if (!(vartok && vartok->variable() && !vartok->variable()->isPointer()))
707716
continue;
708717

709718
ValueFlow::Value value;

test/testvalueflow.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ class TestValueFlow : public TestFixture {
209209
" *x = 0;\n" // <- x can point at i
210210
"}";
211211
ASSERT_EQUALS(true, testValueOfX(code, 4, "& i"));
212+
213+
code = "void f() {\n"
214+
" struct X *x;\n"
215+
" x = &x[1];\n"
216+
"}";
217+
ASSERT_EQUALS(true, tokenValues(code, "&").empty());
218+
ASSERT_EQUALS(true, tokenValues(code, "x [").empty());
212219
}
213220

214221
void valueFlowArrayElement() {

0 commit comments

Comments
 (0)