From 6019f452490b828f2232ecac7648794aebb0994d Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Fri, 3 Jan 2014 23:23:06 +0100 Subject: [PATCH] Ticket #5201: Get rid of internal error when checking valid C input for memory leaks. --- lib/tokenize.cpp | 7 +++++++ test/testmemleak.cpp | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 324cef805..d42a67da6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7020,6 +7020,13 @@ bool Tokenizer::simplifyRedundantParentheses() tok->deleteNext(); ret = true; } + + if (Token::Match(tok->previous(), "[,;{}] ( %var% ) .")) { + // Remove the parentheses + tok->deleteThis(); + tok->deleteNext(); + ret = true; + } if (Token::Match(tok->previous(), "[(,;{}] ( %var% (") && tok->link()->previous() == tok->linkAt(2)) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index dc2a0897c..52b2dc3b3 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -5110,7 +5110,7 @@ public: } private: - void check(const char code[], const char fname[] = 0) { + void check(const char code[], const char fname[] = 0, bool isCPP = true) { // Clear the error buffer.. errout.str(""); @@ -5119,7 +5119,7 @@ private: // Tokenize.. Tokenizer tokenizer(&settings, this); std::istringstream istr(code); - tokenizer.tokenize(istr, fname ? fname : "test.cpp"); + tokenizer.tokenize(istr, fname ? fname : (isCPP ? "test.cpp" : "test.c")); tokenizer.simplifyTokenList2(); // Check for memory leaks.. @@ -5166,6 +5166,7 @@ private: // Segmentation fault in CheckMemoryLeakStructMember TEST_CASE(trac5030); + TEST_CASE(varid); // #5201: Analysis confused by (variable).attribute notation } void err() { @@ -5464,6 +5465,19 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void varid() { // #5201 + check("struct S {\n" + " void *state_check_buff;\n" + "};\n" + "void f() {\n" + " S s;\n" + " (s).state_check_buff = (void* )malloc(1);\n" + " if (s.state_check_buff == 0)\n" + " return;\n" + "}", /*fname=*/0, /*isCPP=*/false); + ASSERT_EQUALS("[test.c:9]: (error) Memory leak: s.state_check_buff\n", errout.str()); + } };