Fixed #5968 (False positive: 'Possible null pointer dereference' when checking null in negated conjuction)

This commit is contained in:
Daniel Marjamäki 2014-07-07 17:48:58 +02:00
parent 26a3bdfe00
commit 987ce5a408
2 changed files with 31 additions and 4 deletions

View File

@ -895,11 +895,32 @@ static void valueFlowAfterCondition(TokenList *tokenlist, ErrorLogger *errorLogg
continue;
}
// start token of conditional code
Token *startToken = nullptr;
if (Token::Match(tok, "==|>=|<=|!") && Token::simpleMatch(top->link(), ") {"))
startToken = top->link()->next();
else if (Token::Match(tok, "%var%|!=") && Token::simpleMatch(top->link()->linkAt(1), "} else {"))
startToken = top->link()->linkAt(1)->tokAt(2);
// based on the comparison, should we check the if or while?
int codeblock = 0;
if (Token::Match(tok, "==|>=|<=|!"))
codeblock = 1;
else if (Token::Match(tok, "%var%|!="))
codeblock = 2;
// determine startToken based on codeblock
if (codeblock > 0) {
// if astParent is "!" we need to invert codeblock
const Token *parent = tok->astParent();
while (parent && parent->str() == "&&")
parent = parent->astParent();
if (parent && parent->str() == "!")
codeblock = (codeblock == 1) ? 2 : 1;
// convert codeblock to a startToken
if (codeblock == 1 && Token::simpleMatch(top->link(), ") {"))
startToken = top->link()->next();
else if (Token::simpleMatch(top->link()->linkAt(1), "} else {"))
startToken = top->link()->linkAt(1)->tokAt(2);
}
bool ok = true;
if (startToken)
ok = valueFlowForward(startToken->next(), startToken->link(), var, varid, values, true, tokenlist, errorLogger, settings);

View File

@ -794,6 +794,12 @@ private:
"}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
code = "void f(int x, int y) {\n"
" if (!(x&&y)) { return; }\n"
" a = x;\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
// if (var)
code = "void f(int x) {\n"
" if (x) { a = x; }\n" // <- x is not 0