Skip to content

Commit

Permalink
Fix 12606 and 12287: FP uninitvar when passing address to function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 authored Apr 10, 2024
1 parent be32239 commit 1fa272c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,8 @@ ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings)
return ExprUsage::NotUsed;
if (Token::simpleMatch(parent, ":") && Token::simpleMatch(parent->astParent(), "?"))
return getExprUsage(parent->astParent(), indirect, settings);
if (isUsedAsBool(tok, settings))
return ExprUsage::NotUsed;
}
if (indirect == 0) {
if (Token::Match(parent, "%cop%|%assign%|++|--") && parent->str() != "=" &&
Expand Down
5 changes: 1 addition & 4 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,14 +1652,11 @@ void CheckUninitVar::valueFlowUninit()
continue;
if (!v->subexpressions.empty() && usage == ExprUsage::PassedByReference)
continue;
bool inconclusive = false;
if (usage == ExprUsage::Used && v->tokvalue && tok->variable() && tok->variable()->isArgument() &&
(!isVariableChangedByFunctionCall(v->tokvalue, v->indirect, mSettings, &inconclusive) || inconclusive))
continue;
if (usage != ExprUsage::Used) {
if (!(Token::Match(tok->astParent(), ". %name% (|[") && uninitderef) &&
isVariableChanged(tok, v->indirect, mSettings))
continue;
bool inconclusive = false;
if (isVariableChangedByFunctionCall(tok, v->indirect, mSettings, &inconclusive) || inconclusive)
continue;
}
Expand Down
10 changes: 9 additions & 1 deletion test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6143,7 +6143,7 @@ class TestUninitVar : public TestFixture {
" increment(n);\n"
" return n;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (warning) Uninitialized variable: i\n", errout_str());

// #11412
valueFlowUninit("void f(int n) {\n"
Expand Down Expand Up @@ -6467,6 +6467,14 @@ class TestUninitVar : public TestFixture {
" if ((success == 1) && (myst.a != 0)) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// #12606
valueFlowUninit("void f(int& r) { if (r) {} }\n"
"void g() {\n"
" int i;\n"
" f(i);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (warning) Uninitialized variable: r\n", errout_str());
}

void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
Expand Down

0 comments on commit 1fa272c

Please sign in to comment.