Fixed #2696 (False positive nullpointer)

This commit is contained in:
Martin Exner 2011-04-03 21:06:42 +02:00 committed by Daniel Marjamäki
parent ed8cdddcf5
commit bc3507118d
2 changed files with 43 additions and 4 deletions

View File

@ -133,10 +133,10 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
unknown = false; unknown = false;
// Dereferencing pointer.. // Dereferencing pointer..
if (Token::Match(tok->tokAt(-3), "!!sizeof [;{}=+-/(,] * %var%")) if (Token::Match(tok->tokAt(-3), "!!sizeof [;{}=+-/(,] * %var%") && Token::Match(tok->tokAt(-3), "!!decltype [;{}=+-/(,] * %var%"))
return true; return true;
if (!Token::simpleMatch(tok->tokAt(-2), "& (") && tok->strAt(-1) != "&" && tok->strAt(-1) != "&&" && Token::Match(tok->next(), ". %var%")) if (!Token::simpleMatch(tok->tokAt(-2), "& (") && !Token::Match(tok->tokAt(-2), "sizeof|decltype (") && tok->strAt(-1) != "&" && tok->strAt(-1) != "&&" && Token::Match(tok->next(), ". %var%"))
return true; return true;
if (Token::Match(tok->previous(), "[;{}=+-/(,] %var% [")) if (Token::Match(tok->previous(), "[;{}=+-/(,] %var% ["))
@ -376,8 +376,8 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
tok1 = tok1->tokAt(3); tok1 = tok1->tokAt(3);
} }
// dereference in function call // dereference in function call (but not sizeof|decltype)
else if (Token::Match(tok1->tokAt(-2), "%var% ( %var% . %var%") || else if ((Token::Match(tok1->tokAt(-2), "%var% ( %var% . %var%") && !Token::Match(tok1->tokAt(-2), "sizeof|decltype ( %var% . %var%")) ||
Token::Match(tok1->previous(), ", %var% . %var%")) Token::Match(tok1->previous(), ", %var% . %var%"))
{ {
// Is the function return value taken by the pointer? // Is the function return value taken by the pointer?

View File

@ -1047,6 +1047,45 @@ private:
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #2696 - false positives nr 1
check("void f()\n"
"{\n"
" struct foo *pFoo = NULL;\n"
" size_t len;\n"
"\n"
" len = sizeof(*pFoo) - sizeof(pFoo->data);\n"
"\n"
" if (pFoo)\n"
" bar();\n"
"}");
ASSERT_EQUALS("", errout.str());
// #2696 - false positives nr 2
check("void f()\n"
"{\n"
" struct foo *pFoo = NULL;\n"
" size_t len;\n"
"\n"
" while (pFoo)\n"
" pFoo = pFoo->next;\n"
"\n"
" len = sizeof(pFoo->data);\n"
"}");
ASSERT_EQUALS("", errout.str());
// #2696 - false positives nr 3
check("void f()\n"
"{\n"
" struct foo *pFoo = NULL;\n"
" size_t len;\n"
"\n"
" while (pFoo)\n"
" pFoo = pFoo->next;\n"
"\n"
" len = decltype(*pFoo);\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
// Test CheckNullPointer::nullConstantDereference // Test CheckNullPointer::nullConstantDereference