Fix 11416: FP nullPointerRedundantCheck for check after loop with break (#4620)

This commit is contained in:
Paul Fultz II 2022-12-08 00:27:06 -06:00 committed by GitHub
parent 4103d05c7f
commit 663a8411dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -281,6 +281,8 @@ struct ReverseTraversal {
const bool inLoop = condTok->astTop() && Token::Match(condTok->astTop()->previous(), "for|while (");
// Evaluate condition of for and while loops first
if (inLoop) {
if (Token::findmatch(tok->link(), "goto|break", tok))
break;
if (condAction.isModified())
break;
valueFlowGenericForward(condTok, analyzer, settings);

View File

@ -139,6 +139,7 @@ private:
TEST_CASE(nullpointer93); // #3929
TEST_CASE(nullpointer94); // #11040
TEST_CASE(nullpointer95); // #11142
TEST_CASE(nullpointer96); // #11416
TEST_CASE(nullpointer_addressOf); // address of
TEST_CASE(nullpointerSwitch); // #2626
TEST_CASE(nullpointer_cast); // #4692
@ -2763,6 +2764,25 @@ private:
ASSERT_EQUALS("", errout.str());
}
void nullpointer96()
{
check("struct S {\n"
" int x;\n"
"};\n"
"S *create_s();\n"
"void test() {\n"
" S *s = create_s();\n"
" for (int i = 0; i < s->x; i++) {\n"
" if (s->x == 17) {\n"
" s = nullptr;\n"
" break;\n"
" }\n"
" }\n"
" if (s) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void nullpointer_addressOf() { // address of
check("void f() {\n"
" struct X *x = 0;\n"