Skip to content

Commit e5d7376

Browse files
Fix #12287 FP uninitvar when passing address to function (#5899)
1 parent bbf5efd commit e5d7376

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1653,11 +1653,14 @@ void CheckUninitVar::valueFlowUninit()
16531653
continue;
16541654
if (!v->subexpressions.empty() && usage == ExprUsage::PassedByReference)
16551655
continue;
1656+
bool inconclusive = false;
1657+
if (usage == ExprUsage::Used && v->tokvalue && tok->variable() && tok->variable()->isArgument() &&
1658+
(!isVariableChangedByFunctionCall(v->tokvalue, v->indirect, mSettings, &inconclusive) || inconclusive))
1659+
continue;
16561660
if (usage != ExprUsage::Used) {
16571661
if (!(Token::Match(tok->astParent(), ". %name% (") && uninitderef) &&
16581662
isVariableChanged(tok, v->indirect, mSettings, mTokenizer->isCPP()))
16591663
continue;
1660-
bool inconclusive = false;
16611664
if (isVariableChangedByFunctionCall(tok, v->indirect, mSettings, &inconclusive) || inconclusive)
16621665
continue;
16631666
}

test/testuninitvar.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6110,7 +6110,7 @@ class TestUninitVar : public TestFixture {
61106110
" increment(n);\n"
61116111
" return n;\n"
61126112
"}\n");
6113-
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (warning) Uninitialized variable: i\n", errout.str());
6113+
ASSERT_EQUALS("", errout.str());
61146114

61156115
// #11412
61166116
valueFlowUninit("void f(int n) {\n"
@@ -6396,6 +6396,16 @@ class TestUninitVar : public TestFixture {
63966396
" return x[0];\n"
63976397
"}\n");
63986398
ASSERT_EQUALS("", errout.str());
6399+
6400+
valueFlowUninit("void f(bool* p) {\n" // #12287
6401+
" if (p)\n"
6402+
" *p = true; \n"
6403+
"}\n"
6404+
"void g() {\n"
6405+
" bool b;\n"
6406+
" f(&b);\n"
6407+
"}\n");
6408+
ASSERT_EQUALS("", errout.str());
63996409
}
64006410

64016411
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
@@ -7714,6 +7724,14 @@ class TestUninitVar : public TestFixture {
77147724
" lookupStem(h, &stem);\n"
77157725
"}\n");
77167726
ASSERT_EQUALS("", errout.str());
7727+
7728+
ctu("void increment(int& i) { ++i; }\n" // #6475
7729+
"int f() {\n"
7730+
" int n;\n"
7731+
" increment(n);\n"
7732+
" return n;\n"
7733+
"}\n");
7734+
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Using argument i that points at uninitialized variable n\n", errout.str());
77177735
}
77187736
};
77197737

0 commit comments

Comments
 (0)