From 3b08712930b0d45d7dd35fdb652c5dafc5253f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 30 Jan 2010 09:33:16 +0100 Subject: [PATCH] Fixed #1005 (improve check: nullpointer dereference not found when it is initialized with NULL-cast) --- lib/tokenize.cpp | 69 +++++++++++++++++++++++++++---------- lib/tokenize.h | 10 +++++- test/testsimplifytokens.cpp | 15 ++++++-- test/testtokenize.cpp | 1 - 4 files changed, 72 insertions(+), 23 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9760d743b..702b5082a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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() { diff --git a/lib/tokenize.h b/lib/tokenize.h index 9562260a2..b6f567701 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -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" diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0e8662b03..5ad45465c 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6bafc7ef9..f01314c66 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2494,7 +2494,6 @@ private: { // ticket #732 const char code[] = "char a [ 2 ] = { '-' } ; memset ( a , '-' , sizeof ( a ) ) ;"; - ASSERT_EQUALS(code, tokenizeAndStringify(code)); }