Fixed #1005 (improve check: nullpointer dereference not found when it is initialized with NULL-cast)

This commit is contained in:
Daniel Marjamäki 2010-01-30 09:33:16 +01:00
parent 882e2225e0
commit 3b08712930
4 changed files with 72 additions and 23 deletions

View File

@ -972,6 +972,12 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s
// Split up variable declarations.
simplifyVarDecl();
// Change initialisation of variable to assignment
simplifyInitVar();
// Remove redundant parantheses
simplifyRedundantParanthesis();
// Handle templates..
simplifyTemplates();
@ -2394,6 +2400,23 @@ bool Tokenizer::simplifyTokenList()
tok->deleteNext();
}
// Replace NULL with 0..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "NULL" || tok->str() == "'\\0'")
{
tok->str("0");
}
else if (tok->isNumber() &&
MathLib::isInt(tok->str()) &&
MathLib::toLongNumber(tok->str()) == 0)
{
tok->str("0");
}
}
simplifyStd();
simplifyNamespaces();
@ -2617,24 +2640,6 @@ bool Tokenizer::simplifyTokenList()
}
}
// Simplify variable declarations
simplifyVarDecl();
// Replace NULL with 0..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "NULL" || tok->str() == "'\\0'")
{
tok->str("0");
}
else if (tok->isNumber() &&
MathLib::isInt(tok->str()) &&
MathLib::toLongNumber(tok->str()) == 0)
{
tok->str("0");
}
}
// Replace pointer casts of 0.. "(char *)0" => "0"
for (Token *tok = _tokens; tok; tok = tok->next())
{
@ -2645,6 +2650,12 @@ bool Tokenizer::simplifyTokenList()
}
}
// Change initialisation of variable to assignment
simplifyInitVar();
// Simplify variable declarations
simplifyVarDecl();
simplifyFunctionParameters();
elseif();
simplifyIfAssign();
@ -4111,6 +4122,28 @@ void Tokenizer::simplifyLogicalOperators()
}
void Tokenizer::simplifyInitVar()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[{};] %type% *| %var% ( %num% ) ;") &&
tok->next()->isStandardType())
{
// goto variable name..
tok = tok->tokAt(2);
if (tok->str() == "*")
tok = tok->next();
// insert '='
tok->insertToken("=");
// remove parantheses..
tok->next()->deleteNext();
tok->next()->next()->deleteNext();
}
}
}
bool Tokenizer::simplifyKnownVariables()
{

View File

@ -145,10 +145,18 @@ private:
void simplifySizeof();
/**
* Simplify variable declarations
* Simplify variable declarations (split up)
*/
void simplifyVarDecl();
/**
* Simplify variable initialization
* ; int *p(0);
* =>
* ; int *p = 0;
*/
void simplifyInitVar();
/**
* insert an "int" after "unsigned" if needed:
* "unsigned i" => "unsigned int i"

View File

@ -183,6 +183,9 @@ private:
// remove "std::" on some standard functions
TEST_CASE(removestd);
// Tokenizer::simplifyInitVar
TEST_CASE(simplifyInitVar);
}
std::string tok(const char code[], bool simplify = true)
@ -1654,7 +1657,7 @@ private:
void ifassign1()
{
ASSERT_EQUALS("; a = b ; if ( a ) { ; }", simplifyIfAssign(";if(a=b);"));
ASSERT_EQUALS("; a = b ( ) ; if ( ( a ) ) { ; }", simplifyIfAssign(";if((a=b()));"));
ASSERT_EQUALS("; a = b ( ) ; if ( a ) { ; }", simplifyIfAssign(";if((a=b()));"));
ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) { ; }", simplifyIfAssign(";if(!(a=b()));"));
ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) { ; }", simplifyIfAssign(";if(!(a->x=b()));"));
ASSERT_EQUALS("A ( ) a = b ; if ( a ) { ; }", simplifyIfAssign("A() if(a=b);"));
@ -1703,7 +1706,7 @@ private:
ASSERT_EQUALS("if ( b ( ) && ! a )", simplifyIfNot("if( b() && 0 == a )"));
ASSERT_EQUALS("if ( ! ( a = b ) )", simplifyIfNot("if((a=b)==0)"));
ASSERT_EQUALS("if ( ! x . y )", simplifyIfNot("if(x.y==0)"));
ASSERT_EQUALS("if ( ( ! x ) )", simplifyIfNot("if((x==0))"));
ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if((x==0))"));
ASSERT_EQUALS("if ( ( ! x ) && ! y )", simplifyIfNot("if((x==0) && y==0)"));
ASSERT_EQUALS("if ( ! ( ! fclose ( fd ) ) )", simplifyIfNot("if(!(fclose(fd) == 0))"));
}
@ -2949,7 +2952,7 @@ private:
"int main ( ) "
"{ "
"; "
"VERIFY ( ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ) ; "
"VERIFY ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
@ -3231,6 +3234,12 @@ private:
ASSERT_EQUALS("; malloc ( 10 ) ;", tok("; std::malloc(10);"));
}
void simplifyInitVar()
{
// ticket #1005 - int *p(0); => int *p = 0;
const char code[] = "void foo() { int *p(0); }";
ASSERT_EQUALS("void foo ( ) { int * p ; p = 0 ; }", tok(code));
}
};
REGISTER_TEST(TestSimplifyTokens)

View File

@ -2494,7 +2494,6 @@ private:
{
// ticket #732
const char code[] = "char a [ 2 ] = { '-' } ; memset ( a , '-' , sizeof ( a ) ) ;";
ASSERT_EQUALS(code, tokenizeAndStringify(code));
}