Fix issue 9441: false positive: new(std::nothrow) and nullPointerRedundantCheck (#2403)

* Fix issue 9441: false positive: new(std::nothrow) and nullPointerRedundantCheck

* Skip new if it is a variable
This commit is contained in:
Paul Fultz II 2019-12-01 05:16:31 -06:00 committed by amai2012
parent 95bbc7a1e2
commit a71f58ffc2
2 changed files with 12 additions and 1 deletions

View File

@ -322,6 +322,8 @@ static bool isNullablePointer(const Token* tok, const Settings* settings)
{
if (!tok)
return false;
if (Token::simpleMatch(tok, "new") && tok->varId() == 0)
return false;
if (astIsPointer(tok))
return true;
if (astIsSmartPointer(tok))

View File

@ -85,7 +85,8 @@ private:
TEST_CASE(nullpointer43); // #9404
TEST_CASE(nullpointer44); // #9395, #9423
TEST_CASE(nullpointer45);
TEST_CASE(nullpointer46); // #6850
TEST_CASE(nullpointer46); // #9441
TEST_CASE(nullpointer47); // #6850
TEST_CASE(nullpointer_addressOf); // address of
TEST_CASE(nullpointerSwitch); // #2626
TEST_CASE(nullpointer_cast); // #4692
@ -1615,6 +1616,14 @@ private:
}
void nullpointer46() {
check("void f() {\n"
" char* p = new(std::nothrow) char[1];\n"
" if( p ) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void nullpointer47() {
check("void f(int *p) {\n"
" if(!p[0]) {}\n"
" const int *const a = p;\n"