Skip to content

Commit ffc2a9d

Browse files
authored
Fix 9735 for valueFlowUninit (#3538)
1 parent 7e2c993 commit ffc2a9d

2 files changed

Lines changed: 25 additions & 24 deletions

File tree

lib/valueflow.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,12 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se
565565
if (Token::Match(tok, ". %var%"))
566566
setTokenValue(tok->next(), value, settings);
567567
ValueFlow::Value pvalue = value;
568-
if (!value.subexpressions.empty()) {
569-
if (Token::Match(parent, ". %var%") && contains(value.subexpressions, parent->next()->str()))
568+
if (!value.subexpressions.empty() && Token::Match(parent, ". %var%")) {
569+
if (contains(value.subexpressions, parent->next()->str()))
570570
pvalue.subexpressions.clear();
571+
else
572+
return;
571573
}
572-
if (!pvalue.subexpressions.empty())
573-
return;
574574
if (parent->isUnaryOp("&")) {
575575
pvalue.indirect++;
576576
setTokenValue(parent, pvalue, settings);

test/testuninitvar.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,26 +4517,27 @@ class TestUninitVar : public TestFixture {
45174517

45184518
void valueFlowUninit() {
45194519
// #9735 - FN
4520-
ctu("typedef struct\n"
4521-
"{\n"
4522-
" int x;\n"
4523-
" unsigned int flag : 1;\n"// bit filed gets never initialized
4524-
"} status;\n"
4525-
"bool foo(const status * const s)\n"
4526-
"{\n"
4527-
" return s->flag;\n"// << uninitvar
4528-
"}\n"
4529-
"void bar(const status * const s)\n"
4530-
"{\n"
4531-
" if( foo(s) == 1) {;}\n"
4532-
"}\n"
4533-
"void f(void)\n"
4534-
"{\n"
4535-
" status s;\n"
4536-
" s.x = 42;\n"
4537-
" bar(&s);\n"
4538-
"}");
4539-
ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (error) Using argument s that points at uninitialized variable s\n", errout.str());
4520+
valueFlowUninit("typedef struct\n"
4521+
"{\n"
4522+
" int x;\n"
4523+
" unsigned int flag : 1;\n" // bit filed gets never initialized
4524+
"} status;\n"
4525+
"bool foo(const status * const s)\n"
4526+
"{\n"
4527+
" return s->flag;\n" // << uninitvar
4528+
"}\n"
4529+
"void bar(const status * const s)\n"
4530+
"{\n"
4531+
" if( foo(s) == 1) {;}\n"
4532+
"}\n"
4533+
"void f(void)\n"
4534+
"{\n"
4535+
" status s;\n"
4536+
" s.x = 42;\n"
4537+
" bar(&s);\n"
4538+
"}");
4539+
ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (error) Uninitialized variable: s->flag\n",
4540+
errout.str());
45404541

45414542
// Ticket #2207 - False negative
45424543
valueFlowUninit("void foo() {\n"

0 commit comments

Comments
 (0)