From d48df980b4d22bcb2035b1ed4edbd42aede5c29a Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 7 Oct 2023 07:01:06 -0500 Subject: [PATCH] Fix 12030: False positive: uninitialized variable, conditional modification, flag (#5520) --- lib/programmemory.cpp | 2 +- test/testuninitvar.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 9ea08ca0a..79524745d 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -386,7 +386,7 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok if (Token::simpleMatch(tok2->previous(), "else {")) tok2 = tok2->linkAt(-2)->previous(); } - if (tok2->str() == "}") { + if (tok2->str() == "}" && !Token::Match(tok2->link()->previous(), "%var% {")) { const Token *cond = getCondTokFromEnd(tok2); const bool inElse = Token::simpleMatch(tok2->link()->previous(), "else {"); if (cond) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index f13536c1c..51d5bc056 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3683,6 +3683,29 @@ private: "}", "test.c"); ASSERT_EQUALS("", errout.str()); + // #12030 + valueFlowUninit("int set(int *x);\n" + "void foo(bool a) {\n" + " bool flag{0};\n" + " int x;\n" + " if (!a) {\n" + " flag = set(&x);\n" + " }\n" + " if (!flag || x==3) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + valueFlowUninit("int set(int *x);\n" + "void foo(bool a) {\n" + " bool flag{0};\n" + " int x;\n" + " if (!a) {\n" + " flag = set(&x);\n" + " }\n" + " if (!flag && x==3) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:8]: (warning) Uninitialized variable: x\n", errout.str()); + valueFlowUninit("int do_something();\n" "int set_st(int *x);\n" "int bar();\n"