From 04ecbba361a47c097a25e2eb98d24c0d9570f487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 15 Nov 2015 23:38:23 +0100 Subject: [PATCH] Fixed #7135 (ValueFlow: Wrong pointer alias set for 'p = &p[x];') --- lib/valueflow.cpp | 11 ++++++++++- test/testvalueflow.cpp | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f0172e71e..8ef950f6e 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -703,7 +703,16 @@ static void valueFlowPointerAlias(TokenList *tokenlist) continue; // child should be some buffer or variable - if (!Token::Match(tok->astOperand1(), "%name%|.|[|;")) + const Token *vartok = tok->astOperand1(); + while (vartok) { + if (vartok->str() == "[") + vartok = vartok->astOperand1(); + else if (vartok->str() == "." || vartok->str() == "::") + vartok = vartok->astOperand2(); + else + break; + } + if (!(vartok && vartok->variable() && !vartok->variable()->isPointer())) continue; ValueFlow::Value value; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 7cf3f6c84..7e1a98f6b 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -209,6 +209,13 @@ private: " *x = 0;\n" // <- x can point at i "}"; ASSERT_EQUALS(true, testValueOfX(code, 4, "& i")); + + code = "void f() {\n" + " struct X *x;\n" + " x = &x[1];\n" + "}"; + ASSERT_EQUALS(true, tokenValues(code, "&").empty()); + ASSERT_EQUALS(true, tokenValues(code, "x [").empty()); } void valueFlowArrayElement() {