Fix 12030: False positive: uninitialized variable, conditional modification, flag (#5520)
This commit is contained in:
parent
1d30b617c1
commit
d48df980b4
|
@ -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) {
|
||||||
|
|
|
@ -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"
|
||||||
|
|
Loading…
Reference in New Issue