Fixed #3358 (False null pointer dereference positive with ternary ?: operator)

This commit is contained in:
Daniel Marjamäki 2011-12-03 13:10:07 +01:00
parent b7988a3dab
commit c90558f730
2 changed files with 13 additions and 2 deletions

View File

@ -867,7 +867,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
std::list<const Token *> var; std::list<const Token *> var;
parseFunctionCall(*tok2, var, 0); parseFunctionCall(*tok2, var, 0);
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it) { for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it) {
if ((*it)->varId() == varid) { if (Token::Match(*it, "%varid% [,)]", varid)) {
nullPointerError(*it, pointerName, linenr, inconclusive); nullPointerError(*it, pointerName, linenr, inconclusive);
break; break;
} }
@ -977,7 +977,7 @@ void CheckNullPointer::nullConstantDereference()
// is one of the var items a NULL pointer? // is one of the var items a NULL pointer?
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it) { for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it) {
if ((*it)->str() == "0") { if (Token::Match(*it, "0 [,)]")) {
nullPointerError(*it); nullPointerError(*it);
} }
} }

View File

@ -1422,6 +1422,12 @@ private:
check(code, true); // inconclusive check(code, true); // inconclusive
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str());
} }
check("void f(char *s) {\n" // #3358
" if (s==0);\n"
" strcpy(a, s?b:c);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
// Test CheckNullPointer::nullConstantDereference // Test CheckNullPointer::nullConstantDereference
@ -1487,6 +1493,11 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: s\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: s\n", errout.str());
check("void f() {\n"
" char *s = 0;\n"
" printf(\"%s\", s == 0 ? a : s);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n" check("void f() {\n"
" printf(\"%u%s\", 0, 0);\n" " printf(\"%u%s\", 0, 0);\n"