Skip to content

Commit 5490fad

Browse files
committed
Fixed danmar#5840 (False positive (inconclusive): Possible nullpointer dereference - use before for-loop over nested list)
1 parent 6b78ae7 commit 5490fad

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,16 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
468468
if (tok2->astParent() || tok2->str() != "(" || !Token::simpleMatch(tok2->link(), ") {"))
469469
continue;
470470

471+
// Variable changed in 3rd for-expression
472+
if (Token::simpleMatch(tok2->previous(), "for (")) {
473+
if (isVariableChanged(tok2->astOperand2()->astOperand2(), tok2->link(), varid)) {
474+
varid = 0U;
475+
if (settings->debugwarnings)
476+
bailout(tokenlist, errorLogger, tok, "variable " + var->name() + " used in loop");
477+
}
478+
}
479+
480+
// Variable changed in loop code
471481
if (Token::Match(tok2->previous(), "for|while (")) {
472482
const Token * const start = tok2->link()->next();
473483
const Token * const end = start->link();

test/testvalueflow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,12 @@ class TestValueFlow : public TestFixture {
403403
"}";
404404
ASSERT_EQUALS(false, testValueOfX(code, 2U, 37));
405405

406+
code = "void f(int x) {\n"
407+
" a = x;\n"
408+
" for (; x!=1; x++) { }\n"
409+
"}";
410+
ASSERT_EQUALS(false, testValueOfX(code, 2U, 1));
411+
406412
code = "void f(menu *x) {\n"
407413
" a = x->parent;\n"
408414
" for (i=0;(i<10) && (x!=0); i++) { x = x->next; }\n"

0 commit comments

Comments
 (0)