Fix false positives for delete of known nulls
This commit is contained in:
parent
2c64d299ca
commit
527236b321
|
@ -217,6 +217,10 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
|
||||||
if (Token::Match(tok->previous(), "[;{}] %var% ="))
|
if (Token::Match(tok->previous(), "[;{}] %var% ="))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// OK to delete a null
|
||||||
|
if (Token::Match(tok->previous(), "delete %var%") || Token::Match(tok->tokAt(-3), "delete [ ] %var%"))
|
||||||
|
return false;
|
||||||
|
|
||||||
// unknown if it's a dereference
|
// unknown if it's a dereference
|
||||||
unknown = true;
|
unknown = true;
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ private:
|
||||||
TEST_CASE(nullpointer_in_return);
|
TEST_CASE(nullpointer_in_return);
|
||||||
TEST_CASE(nullpointer_in_typeid);
|
TEST_CASE(nullpointer_in_typeid);
|
||||||
TEST_CASE(nullpointer_in_for_loop);
|
TEST_CASE(nullpointer_in_for_loop);
|
||||||
|
TEST_CASE(nullpointerDelete);
|
||||||
}
|
}
|
||||||
|
|
||||||
void check(const char code[], bool inconclusive = false, bool cpp11 = false) {
|
void check(const char code[], bool inconclusive = false, bool cpp11 = false) {
|
||||||
|
@ -1587,6 +1588,25 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void nullpointerDelete() {
|
||||||
|
check("void f() {\n"
|
||||||
|
" K *k = getK();\n"
|
||||||
|
" if (k)\n"
|
||||||
|
" k->doStuff();\n"
|
||||||
|
" delete k;\n"
|
||||||
|
"}\n", true);
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("void f() {\n"
|
||||||
|
" K *k = getK();\n"
|
||||||
|
" if (k)\n"
|
||||||
|
" k[0] = ptr;\n"
|
||||||
|
" delete [] k;\n"
|
||||||
|
" k = new K[10];\n"
|
||||||
|
"}\n", true);
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_TEST(TestNullPointer)
|
REGISTER_TEST(TestNullPointer)
|
||||||
|
|
Loading…
Reference in New Issue