From 1c28457d2c2317070af45f0650d9586f85a13b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 10 Jun 2023 15:22:17 +0200 Subject: [PATCH] ValueFlow: Fix uninitvar false positive after initialization '*((int*)&x) = ..' (#5142) --- lib/valueflow.cpp | 8 ++++++++ test/testvalueflow.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 16dfc2725..7e5f9f667 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7924,6 +7924,14 @@ static Token* findStartToken(const Variable* var, Token* start, const Library* l Token* first = uses.front(); if (Token::findmatch(start, "goto|asm|setjmp|longjmp", first)) return start; + if (first != var->nameToken()) { + // if this is lhs in assignment then set first to the first token in LHS expression + Token* temp = first; + while (Token::Match(temp->astParent(), "[&*(]") && precedes(temp->astParent(), temp)) + temp = temp->astParent(); + if (Token::simpleMatch(temp->astParent(), "=") && precedes(temp, temp->astParent())) + first = temp; + } // If there is only one usage if (uses.size() == 1) return first->previous(); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 17e4278d9..1f5cb95a8 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -5292,6 +5292,18 @@ private: "}"; ASSERT_EQUALS(0U, tokenValues(code, "x )").size()); + // initialization + code = "int foo() {\n" + " int x;\n" + " *((int *)(&x)) = 12;" + " a = x + 1;\n" + "}"; + values = tokenValues(code, "x +"); + ASSERT_EQUALS(true, values.empty()); + // ASSERT_EQUALS(1U, values.size()); + // ASSERT(values.front().isIntValue()); + // ASSERT_EQUALS(12, values.front().intvalue); + // #8036 code = "void foo() {\n" " int x;\n"