Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12082 FP uninitvar for assignment to array member in conditional #5570

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading