Fix 12030: False positive: uninitialized variable, conditional modification, flag (#5520)

This commit is contained in:
Paul Fultz II 2023-10-07 07:01:06 -05:00 committed by GitHub
parent 1d30b617c1
commit d48df980b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -386,7 +386,7 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok
if (Token::simpleMatch(tok2->previous(), "else {")) if (Token::simpleMatch(tok2->previous(), "else {"))
tok2 = tok2->linkAt(-2)->previous(); tok2 = tok2->linkAt(-2)->previous();
} }
if (tok2->str() == "}") { if (tok2->str() == "}" && !Token::Match(tok2->link()->previous(), "%var% {")) {
const Token *cond = getCondTokFromEnd(tok2); const Token *cond = getCondTokFromEnd(tok2);
const bool inElse = Token::simpleMatch(tok2->link()->previous(), "else {"); const bool inElse = Token::simpleMatch(tok2->link()->previous(), "else {");
if (cond) { if (cond) {

View File

@ -3683,6 +3683,29 @@ private:
"}", "test.c"); "}", "test.c");
ASSERT_EQUALS("", errout.str()); 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" valueFlowUninit("int do_something();\n"
"int set_st(int *x);\n" "int set_st(int *x);\n"
"int bar();\n" "int bar();\n"