Fixed #3922 (false positive: (error) null pointer dereference)

This commit is contained in:
Daniel Marjamäki 2012-08-18 22:11:48 +02:00
parent 6a5cc4727d
commit 7786e12ba2
2 changed files with 33 additions and 0 deletions

View File

@ -3395,6 +3395,8 @@ bool Tokenizer::simplifyTokenList()
modified |= simplifyCalculations();
}
simplifyConditionOperator();
// replace strlen(str)
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "strlen ( %str% )")) {
@ -4144,6 +4146,28 @@ void Tokenizer::simplifyCompoundAssignment()
void Tokenizer::simplifyConditionOperator()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok,"return|= ( true|false ) ?")) {
Token *tok2 = tok->tokAt(5);
while (tok2 && (tok2->isName() || tok2->isNumber() || tok2->isArithmeticalOp()))
tok2 = tok2->next();
if (tok2 && tok2->str() == ":") {
if (tok->strAt(2) == "false") {
Token::eraseTokens(tok,tok2->next());
} else {
Token *tok3 = tok2->next();
while (tok3 && (tok3->isName() || tok3->isNumber() || tok3->isArithmeticalOp()))
tok3 = tok3->next();
if (tok3 && tok3->str() == ";") {
tok->deleteNext(4);
tok = tok2;
while (tok && tok->str() != ";")
tok->deleteThis();
}
}
}
}
if (tok->str() == "(")
tok = tok->link();
else if (tok->str() == ")")

View File

@ -2792,6 +2792,15 @@ private:
"1: ; char * p@1 ; if ( a ) { * p@1 = 1 ; } else { * p@1 = 0 ; }\n";
ASSERT_EQUALS(expected, tokenizeDebugListing(code, true));
}
{
// #3922 - (true)
ASSERT_EQUALS("; x = 2 ;", tok("; x = (true)?2:4;"));
ASSERT_EQUALS("; x = 4 ;", tok("; x = (false)?2:4;"));
ASSERT_EQUALS("; x = * a ;", tok("; x = (true)?*a:*b;"));
ASSERT_EQUALS("; x = * b ;", tok("; x = (false)?*a:*b;"));
ASSERT_EQUALS("void f ( ) { return 1 ; }", tok("void f() { char *p=0; return (p==0)?1:2; }"));
}
}
void calculations() {