Null pointer: Fixed false positive discovered by Travis when self-checking cppcheck

This commit is contained in:
Daniel Marjamäki 2014-04-02 19:39:42 +02:00
parent 189360ae20
commit a6557f8c74
2 changed files with 9 additions and 3 deletions

View File

@ -735,7 +735,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
nullPointerError(tok2, pointerName, vartok, inconclusive); nullPointerError(tok2, pointerName, vartok, inconclusive);
else if (unknown) else if (unknown)
nullPointerError(tok2, pointerName, vartok, true); nullPointerError(tok2, pointerName, vartok, true);
if (Token::Match(tok2, "%var% ?")) if (Token::Match(tok2, "%var% %oror%|&&|?"))
break; break;
} }
} }

View File

@ -1726,6 +1726,12 @@ private:
" return q ? p->x : 0;\n" " return q ? p->x : 0;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
check("int f(ABC *p) {\n" // FP : return &&
" if (!p) {}\n"
" return p && p->x;\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
// Test CheckNullPointer::nullConstantDereference // Test CheckNullPointer::nullConstantDereference
@ -2215,8 +2221,8 @@ private:
} }
void functioncalllibrary() { void functioncalllibrary() {
Settings settings; Settings settings1;
Tokenizer tokenizer(&settings,this); Tokenizer tokenizer(&settings1,this);
std::istringstream code("void f() { int a,b; x(a,b); }"); std::istringstream code("void f() { int a,b; x(a,b); }");
tokenizer.tokenize(code,"test.c"); tokenizer.tokenize(code,"test.c");
const Token *xtok = Token::findsimplematch(tokenizer.tokens(), "x"); const Token *xtok = Token::findsimplematch(tokenizer.tokens(), "x");