From ffc2a9d8e2443c57275aa1a2dd2a0cd6e88828bb Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 6 Nov 2021 13:06:07 -0500 Subject: [PATCH] Fix 9735 for valueFlowUninit (#3538) --- lib/valueflow.cpp | 8 ++++---- test/testuninitvar.cpp | 41 +++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 8df0ec091..029483c11 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -565,12 +565,12 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se if (Token::Match(tok, ". %var%")) setTokenValue(tok->next(), value, settings); ValueFlow::Value pvalue = value; - if (!value.subexpressions.empty()) { - if (Token::Match(parent, ". %var%") && contains(value.subexpressions, parent->next()->str())) + if (!value.subexpressions.empty() && Token::Match(parent, ". %var%")) { + if (contains(value.subexpressions, parent->next()->str())) pvalue.subexpressions.clear(); + else + return; } - if (!pvalue.subexpressions.empty()) - return; if (parent->isUnaryOp("&")) { pvalue.indirect++; setTokenValue(parent, pvalue, settings); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 22375664e..6966dee36 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -4517,26 +4517,27 @@ private: void valueFlowUninit() { // #9735 - FN - ctu("typedef struct\n" - "{\n" - " int x;\n" - " unsigned int flag : 1;\n"// bit filed gets never initialized - "} status;\n" - "bool foo(const status * const s)\n" - "{\n" - " return s->flag;\n"// << uninitvar - "}\n" - "void bar(const status * const s)\n" - "{\n" - " if( foo(s) == 1) {;}\n" - "}\n" - "void f(void)\n" - "{\n" - " status s;\n" - " s.x = 42;\n" - " bar(&s);\n" - "}"); - ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (error) Using argument s that points at uninitialized variable s\n", errout.str()); + valueFlowUninit("typedef struct\n" + "{\n" + " int x;\n" + " unsigned int flag : 1;\n" // bit filed gets never initialized + "} status;\n" + "bool foo(const status * const s)\n" + "{\n" + " return s->flag;\n" // << uninitvar + "}\n" + "void bar(const status * const s)\n" + "{\n" + " if( foo(s) == 1) {;}\n" + "}\n" + "void f(void)\n" + "{\n" + " status s;\n" + " s.x = 42;\n" + " bar(&s);\n" + "}"); + ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (error) Uninitialized variable: s->flag\n", + errout.str()); // Ticket #2207 - False negative valueFlowUninit("void foo() {\n"