From 26c193f9bc8dc01bd05f87c654dc919d9c20d67d Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Fri, 1 May 2009 21:31:07 +0300 Subject: [PATCH] Fix ticket #204 (false positive::memory leak with --all when free is guarded by simple if) http://apps.sourceforge.net/trac/cppcheck/ticket/204 --- src/tokenize.cpp | 5 +++- test/testtokenize.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index eacd46069..d8fe194fc 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1979,6 +1979,7 @@ bool Tokenizer::simplifyIfNot() bool Tokenizer::simplifyKnownVariables() { + createLinks(); bool ret = false; for (Token *tok = _tokens; tok; tok = tok->next()) { @@ -2013,7 +2014,9 @@ bool Tokenizer::simplifyKnownVariables() for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next()) { // Perhaps it's a loop => bail out - if (Token::Match(tok3, "[{}]")) + if (tok3->str() == "{" && Token::Match(tok3->previous(), ")")) + break; + else if (tok3->str() == "}" && Token::Match(tok3->link()->previous(), ")")) break; // Variable is used somehow in a non-defined pattern => bail out diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 5d16e981f..987401d35 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -90,6 +90,7 @@ private: TEST_CASE(simplifyKnownVariables7); TEST_CASE(simplifyKnownVariables8); TEST_CASE(simplifyKnownVariables9); + TEST_CASE(simplifyKnownVariables10); TEST_CASE(multiCompare); @@ -634,7 +635,64 @@ private: ASSERT_EQUALS(std::string(" void foo ( ) { int a ; a = 1 ; int b ; b = 2 ; if ( 1 < 2 ) ; }"), ostr.str()); } + void simplifyKnownVariables10() + { + { + const char code[] = "void f()\n" + "{\n" + " bool b=false;\n" + "\n" + " {\n" + " b = true;\n" + " }\n" + "\n" + " if( b )\n" + " {\n" + " a();\n" + " }\n" + "}\n"; + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(std::string(" void f ( ) { bool b ; b = false ; { b = true ; } if ( true ) { a ( ) ; } }"), ostr.str()); + } + + { + const char code[] = "void f()\n" + "{\n" + " bool b=false;\n" + " { b = false; }\n" + " {\n" + " b = true;\n" + " }\n" + "\n" + " if( b )\n" + " {\n" + " a();\n" + " }\n" + "}\n"; + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(std::string(" void f ( ) { bool b ; b = false ; { b = false ; } { b = true ; } if ( true ) { a ( ) ; } }"), ostr.str()); + } + } void multiCompare() {