Skip to content

Commit 9988cd5

Browse files
Partial fix for #6710 FN nullPointerRedundantCheck after pointer usage in for init statement (#8471)
1 parent 2c03f19 commit 9988cd5

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

lib/reverseanalyzer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ namespace {
110110
{
111111
if (Token::simpleMatch(tok->tokAt(-2), "} else {"))
112112
tok = tok->linkAt(-2);
113-
if (Token::simpleMatch(tok->previous(), ") {"))
113+
if (Token::simpleMatch(tok->previous(), ") {")) {
114+
if (Token::simpleMatch(tok->linkAt(-1)->astOperand2(), ";"))
115+
return tok->linkAt(-1)->astOperand2();
114116
return tok->linkAt(-1);
117+
}
115118
if (Token::simpleMatch(tok->previous(), "do {"))
116119
return tok->previous();
117120
return tok;
@@ -232,7 +235,7 @@ namespace {
232235
if (!Token::Match(assignTop->astOperand1(), "%assign%")) {
233236
continueB &= updateRecursive(assignTop->astOperand1());
234237
}
235-
if (!assignTop->astParent())
238+
if (!assignTop->astParent() || Token::simpleMatch(assignTop->astParent(), ";"))
236239
break;
237240
assignTop = assignTop->astParent();
238241
}

test/testnullpointer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,6 +3710,19 @@ class TestNullPointer : public TestFixture {
37103710
" i++;\n"
37113711
"}\n");
37123712
ASSERT_EQUALS("", errout_str());
3713+
3714+
check("void f(const int* p) {\n" // #6710
3715+
" for (int i = *p; i < 5; ++i) {}\n"
3716+
" if (p) {}\n"
3717+
"}\n"
3718+
"struct S { int a; };\n"
3719+
"void g(const S* s) {\n"
3720+
" for (int i = s->a; i < 5; ++i) {}\n"
3721+
" if (s) {}\n"
3722+
"}\n");
3723+
ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:19]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n"
3724+
"[test.cpp:8:9] -> [test.cpp:7:18]: (warning) Either the condition 's' is redundant or there is possible null pointer dereference: s. [nullPointerRedundantCheck]\n",
3725+
errout_str());
37133726
}
37143727

37153728
void nullpointerDeadCode() {

0 commit comments

Comments
 (0)