From d19dd2c61d69cffd63b655795845c4e0c10586a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 7 May 2010 18:37:50 +0200 Subject: [PATCH] Fixed #1654 (False positive: Memory leak with: ( delete ( p ) , ( p ) = 0 );) --- lib/tokenize.cpp | 8 ++++++++ test/testtokenize.cpp | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index bd5b76573..15f524419 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5275,6 +5275,14 @@ bool Tokenizer::simplifyRedundantParanthesis() ret = true; } + if ((Token::simpleMatch(tok->previous(), "delete (") && Token::Match(tok->link(), ") ;|,")) || + (Token::simpleMatch(tok->previous(), "; (") && Token::Match(tok->link(), ") ;"))) + { + tok->link()->deleteThis(); + tok->deleteThis(); + ret = true; + } + if (Token::Match(tok->previous(), "[(!*;{}] ( %var% )") && tok->next()->varId() != 0) { // We have "( var )", remove the paranthesis diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index c35a2c40a..18a5ab9b2 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -157,6 +157,7 @@ private: TEST_CASE(removeParantheses4); // Ticket #390 TEST_CASE(removeParantheses5); // Ticket #392 TEST_CASE(removeParantheses6); + TEST_CASE(removeParantheses7); TEST_CASE(tokenize_double); TEST_CASE(tokenize_strings); @@ -186,7 +187,7 @@ private: // unsigned i; => unsigned int i; TEST_CASE(unsigned1); TEST_CASE(unsigned2); - TEST_CASE(unsigned3); // template arguments + TEST_CASE(unsigned3); // template arguments TEST_CASE(testUpdateClassList); TEST_CASE(createLinks); @@ -2475,6 +2476,23 @@ private: ASSERT_EQUALS(" ( ! abc . a )", ostr.str()); } + void removeParantheses7() + { + const char code[] = ";(delete(p), (p)=0);"; + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.simplifyTokenList(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" ; delete p ; ( p ) = 0 ;", ostr.str()); + } + void tokenize_double() { const char code[] = "void f()\n"