Skip to content

Commit a71f58f

Browse files
pfultz2amai2012
authored andcommitted
Fix issue 9441: false positive: new(std::nothrow) and nullPointerRedundantCheck (cppcheck-opensource#2403)
* Fix issue 9441: false positive: new(std::nothrow) and nullPointerRedundantCheck * Skip new if it is a variable
1 parent 95bbc7a commit a71f58f

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/checknullpointer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ static bool isNullablePointer(const Token* tok, const Settings* settings)
322322
{
323323
if (!tok)
324324
return false;
325+
if (Token::simpleMatch(tok, "new") && tok->varId() == 0)
326+
return false;
325327
if (astIsPointer(tok))
326328
return true;
327329
if (astIsSmartPointer(tok))

test/testnullpointer.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class TestNullPointer : public TestFixture {
8585
TEST_CASE(nullpointer43); // #9404
8686
TEST_CASE(nullpointer44); // #9395, #9423
8787
TEST_CASE(nullpointer45);
88-
TEST_CASE(nullpointer46); // #6850
88+
TEST_CASE(nullpointer46); // #9441
89+
TEST_CASE(nullpointer47); // #6850
8990
TEST_CASE(nullpointer_addressOf); // address of
9091
TEST_CASE(nullpointerSwitch); // #2626
9192
TEST_CASE(nullpointer_cast); // #4692
@@ -1615,6 +1616,14 @@ class TestNullPointer : public TestFixture {
16151616
}
16161617

16171618
void nullpointer46() {
1619+
check("void f() {\n"
1620+
" char* p = new(std::nothrow) char[1];\n"
1621+
" if( p ) {}\n"
1622+
"}\n");
1623+
ASSERT_EQUALS("", errout.str());
1624+
}
1625+
1626+
void nullpointer47() {
16181627
check("void f(int *p) {\n"
16191628
" if(!p[0]) {}\n"
16201629
" const int *const a = p;\n"

0 commit comments

Comments
 (0)