Fix issue 10178: FP: nullPointerRedundantCheck with ternary and null condition first (#3134)

This commit is contained in:
Paul Fultz II 2021-02-17 05:09:11 -06:00 committed by GitHub
parent 7ab84bef0c
commit 0619b873d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -78,8 +78,6 @@ struct ReverseTraversal {
int opSide = 0;
for (; tok && tok->astParent(); tok = tok->astParent()) {
Token* parent = tok->astParent();
if (tok != parent->astOperand2())
continue;
if (Token::simpleMatch(parent, ":")) {
if (astIsLHS(tok))
opSide = 1;
@ -88,6 +86,8 @@ struct ReverseTraversal {
else
opSide = 0;
}
if (tok != parent->astOperand2())
continue;
if (!Token::Match(parent, "%oror%|&&|?"))
continue;
Token* condTok = parent->astOperand1();
@ -103,9 +103,9 @@ struct ReverseTraversal {
}
if (parent->str() == "?") {
if (!checkElse && opSide == 1)
if (checkElse && opSide == 1)
return parent;
if (!checkThen && opSide == 2)
if (checkThen && opSide == 2)
return parent;
}
if (!checkThen && parent->str() == "&&")
@ -128,6 +128,8 @@ struct ReverseTraversal {
break;
if (Token::Match(tok, "%name% :"))
break;
if (Token::simpleMatch(tok, ":"))
continue;
// Evaluate LHS of assignment before RHS
if (Token* assignTok = assignExpr(tok)) {
// If assignTok has broken ast then stop

View File

@ -111,6 +111,7 @@ private:
TEST_CASE(nullpointer68);
TEST_CASE(nullpointer69); // #8143
TEST_CASE(nullpointer70);
TEST_CASE(nullpointer71); // #10178
TEST_CASE(nullpointer_addressOf); // address of
TEST_CASE(nullpointerSwitch); // #2626
TEST_CASE(nullpointer_cast); // #4692
@ -2212,6 +2213,24 @@ private:
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:10]: (warning) Either the condition 'first' is redundant or there is possible null pointer dereference: first.\n", errout.str());
}
void nullpointer71() {
check("void f() {\n"
" Device* dev = Get();\n"
" SetCount(dev == nullptr ? 0 : dev->size());\n"
" if (dev)\n"
" DoSomething(dev);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" Device* dev = Get();\n"
" SetCount(dev != nullptr ? dev->size() : 0);\n"
" if (dev)\n"
" DoSomething(dev);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void nullpointer_addressOf() { // address of
check("void f() {\n"
" struct X *x = 0;\n"