Skip to content

Commit 9ef0910

Browse files
authored
Fix 14137: False positive: uninitvar when using an assert before loop (#7828)
1 parent f48ffc1 commit 9ef0910

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/programmemory.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok
441441
pm.setValue(vartok, execute(valuetok, pm, settings));
442442
}
443443
}
444+
} else if (Token::simpleMatch(tok2, ")") && tok2->link() &&
445+
Token::Match(tok2->link()->previous(), "assert|ASSERT ( !!)")) {
446+
const Token* cond = tok2->link()->astOperand2();
447+
if (!conditionIsTrue(cond, state, settings)) {
448+
// TODO: change to assert when we can propagate the assert, for now just bail
449+
if (conditionIsFalse(cond, state, settings))
450+
return;
451+
programMemoryParseCondition(pm, cond, nullptr, settings, true);
452+
}
453+
tok2 = tok2->link()->previous();
444454
} else if (tok2->exprId() > 0 && Token::Match(tok2, ".|(|[|*|%var%") && !pm.hasValue(tok2->exprId()) &&
445455
isVariableChanged(tok2, 0, settings)) {
446456
pm.setUnknown(tok2);

test/testuninitvar.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7667,6 +7667,16 @@ class TestUninitVar : public TestFixture {
76677667
" }\n"
76687668
"}\n");
76697669
ASSERT_EQUALS("[test.cpp:3:24]: (error) Uninitialized variable: b [uninitvar]\n", errout_str());
7670+
7671+
// #14137
7672+
valueFlowUninit("int f(int n) {\n"
7673+
" int x;\n"
7674+
" assert(n > 0);\n"
7675+
" for(int i=0;i<n;i++)\n"
7676+
" x = n;\n"
7677+
" return x;\n"
7678+
"}\n");
7679+
ASSERT_EQUALS("", errout_str());
76707680
}
76717681

76727682
void uninitvar_memberfunction() {

0 commit comments

Comments
 (0)