From f6399c4cba7a978ed26fbb75d13db061dcee462f Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 20 Nov 2020 02:36:09 -0600 Subject: [PATCH] Fix issue 9980: FP nullPointerRedundantCheck - condition after while loop (#2912) --- lib/reverseanalyzer.cpp | 2 +- test/testnullpointer.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/reverseanalyzer.cpp b/lib/reverseanalyzer.cpp index f698dacee..a6a86f63e 100644 --- a/lib/reverseanalyzer.cpp +++ b/lib/reverseanalyzer.cpp @@ -173,7 +173,7 @@ struct ReverseTraversal { if (!continueB) break; valueFlowGenericForward(assignTop->astOperand2(), analyzer, settings); - tok = previousBeforeAstLeftmostLeaf(assignTop); + tok = previousBeforeAstLeftmostLeaf(assignTop)->next(); continue; } if (tok->str() == "}") { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 86cbd1c93..94d772911 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -105,6 +105,7 @@ private: TEST_CASE(nullpointer62); TEST_CASE(nullpointer63); TEST_CASE(nullpointer64); + TEST_CASE(nullpointer65); // #9980 TEST_CASE(nullpointer_addressOf); // address of TEST_CASE(nullpointerSwitch); // #2626 TEST_CASE(nullpointer_cast); // #4692 @@ -2011,6 +2012,26 @@ private: ASSERT_EQUALS("", errout.str()); } + void nullpointer65() { + check("struct A {\n" + " double get();\n" + "};\n" + "double x;\n" + "double run(A** begin, A** end) {\n" + " A* a = nullptr;\n" + " while (begin != end) {\n" + " a = *begin;\n" + " x = a->get();\n" + " ++begin;\n" + " }\n" + " x = 0;\n" + " if (a)\n" + " return a->get();\n" + " return 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void nullpointer_addressOf() { // address of check("void f() {\n" " struct X *x = 0;\n"