Skip to content

Commit f6399c4

Browse files
authored
Fix issue 9980: FP nullPointerRedundantCheck - condition after while loop (cppcheck-opensource#2912)
1 parent 317a2d0 commit f6399c4

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/reverseanalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct ReverseTraversal {
173173
if (!continueB)
174174
break;
175175
valueFlowGenericForward(assignTop->astOperand2(), analyzer, settings);
176-
tok = previousBeforeAstLeftmostLeaf(assignTop);
176+
tok = previousBeforeAstLeftmostLeaf(assignTop)->next();
177177
continue;
178178
}
179179
if (tok->str() == "}") {

test/testnullpointer.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class TestNullPointer : public TestFixture {
105105
TEST_CASE(nullpointer62);
106106
TEST_CASE(nullpointer63);
107107
TEST_CASE(nullpointer64);
108+
TEST_CASE(nullpointer65); // #9980
108109
TEST_CASE(nullpointer_addressOf); // address of
109110
TEST_CASE(nullpointerSwitch); // #2626
110111
TEST_CASE(nullpointer_cast); // #4692
@@ -2011,6 +2012,26 @@ class TestNullPointer : public TestFixture {
20112012
ASSERT_EQUALS("", errout.str());
20122013
}
20132014

2015+
void nullpointer65() {
2016+
check("struct A {\n"
2017+
" double get();\n"
2018+
"};\n"
2019+
"double x;\n"
2020+
"double run(A** begin, A** end) {\n"
2021+
" A* a = nullptr;\n"
2022+
" while (begin != end) {\n"
2023+
" a = *begin;\n"
2024+
" x = a->get();\n"
2025+
" ++begin;\n"
2026+
" }\n"
2027+
" x = 0;\n"
2028+
" if (a)\n"
2029+
" return a->get();\n"
2030+
" return 0;\n"
2031+
"}\n");
2032+
ASSERT_EQUALS("", errout.str());
2033+
}
2034+
20142035
void nullpointer_addressOf() { // address of
20152036
check("void f() {\n"
20162037
" struct X *x = 0;\n"

0 commit comments

Comments
 (0)