Skip to content

Commit

Permalink
Fix #12082 FP uninitvar for assignment to array member in conditional (
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Oct 18, 2023
1 parent 285ef96 commit 09f426d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3249,9 +3249,11 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Settings

ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings, bool cpp)
{
const Token* const parent = tok->astParent();
const Token* parent = tok->astParent();
if (indirect > 0 && parent) {
if (Token::Match(parent, "%assign%") && astIsRHS(tok))
while (Token::simpleMatch(parent, "[") && parent->astParent())
parent = parent->astParent();
if (Token::Match(parent, "%assign%") && (astIsRHS(tok) || astIsLHS(parent->astOperand1())))
return ExprUsage::NotUsed;
if (parent->isConstOp())
return ExprUsage::NotUsed;
Expand Down
8 changes: 8 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6289,6 +6289,14 @@ class TestUninitVar : public TestFixture {
" foo(q);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: q\n", errout.str());

valueFlowUninit("int g();\n" // #12082
"void f() {\n"
" int a[1], b[1];\n"
" while (a[0] = g()) {}\n"
" if ((b[0] = g()) == 0) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
}

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

0 comments on commit 09f426d

Please sign in to comment.