From 985ac662eefab908c2e73742ec8376d0b8615766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 25 Aug 2012 12:00:25 +0200 Subject: [PATCH] Fixed #4035 (False positive: Memory leak: pTempFile) --- lib/tokenize.cpp | 20 ++++++++------------ lib/tokenize.h | 2 -- test/testnullpointer.cpp | 25 +++++++++++-------------- test/testtokenize.cpp | 17 +++++++++++------ 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7bff5ffcf..37b6c1441 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1995,8 +1995,6 @@ bool Tokenizer::tokenize(std::istream &code, removeRedundantSemicolons(); - simplifyReservedWordNullptr(); - simplifyParameterVoid(); simplifyRedundantConsecutiveBraces(); @@ -2106,6 +2104,14 @@ void Tokenizer::simplifyNull() tok->str("0"); } } + + // nullptr.. + if (isCPP()) { + for (Token *tok = list.front(); tok; tok = tok->next()) { + if (tok->str() == "nullptr") + tok->str("0"); + } + } } void Tokenizer::concatenateNegativeNumber() @@ -2198,16 +2204,6 @@ void Tokenizer::simplifyParameterVoid() } } -void Tokenizer::simplifyReservedWordNullptr() -{ - if (_settings->standards.cpp11) { - for (Token *tok = list.front(); tok; tok = tok->next()) { - if (tok->str() == "nullptr") - tok->str("0"); - } - } -} - void Tokenizer::simplifyRedundantConsecutiveBraces() { // Remove redundant consecutive braces, i.e. '.. { { .. } } ..' -> '.. { .. } ..'. diff --git a/lib/tokenize.h b/lib/tokenize.h index 5a1e7cbdf..a6e8a582d 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -463,8 +463,6 @@ public: bool hasComplicatedSyntaxErrorsInTemplates(); - void simplifyReservedWordNullptr(); - /** * Simplify e.g. 'atol("0")' into '0' */ diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index beb7ff2cc..ca9de7087 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -74,19 +74,18 @@ private: TEST_CASE(crash1); } - void check(const char code[], bool inconclusive = false, bool cpp11 = false) { + void check(const char code[], bool inconclusive = false, const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str(""); Settings settings; settings.addEnabled("style"); settings.inconclusive = inconclusive; - settings.standards.cpp11 = cpp11; // Tokenize.. Tokenizer tokenizer(&settings, this); std::istringstream istr(code); - if (!tokenizer.tokenize(istr, "test.cpp")) + if (!tokenizer.tokenize(istr, filename)) return; // Check for redundant code.. @@ -1159,19 +1158,17 @@ private: ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", errout.str()); } - void nullpointer12() { // ticket #2470 - check("int foo()\n" - "{\n" - " int* i = nullptr;\n" - " return *i;\n" - "}\n", false, true); // Check as C++11 code + void nullpointer12() { // ticket #2470, #4035 + const char code[] = "int foo()\n" + "{\n" + " int* i = nullptr;\n" + " return *i;\n" + "}\n"; + + check(code, false, "test.cpp"); // C++ file => nullptr means NULL ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); - check("int foo(int nullptr)\n" - "{\n" - " int* i = nullptr;\n" - " return *i;\n" - "}", true); + check(code, false, "test.c"); // C file => nullptr does not mean NULL ASSERT_EQUALS("", errout.str()); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b9c804a11..6d3789fa9 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4646,12 +4646,17 @@ private: } void simplify_null() { - const char code[] = - "int * p = NULL;\n" - "int * q = __null;\n"; - const char expected[] = - "int * p ; p = 0 ;\nint * q ; q = 0 ;"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); + { + const char code[] = + "int * p = NULL;\n" + "int * q = __null;\n"; + const char expected[] = + "int * p ; p = 0 ;\nint * q ; q = 0 ;"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); + } + + ASSERT_EQUALS("( a == nullptr )", tokenizeAndStringify("(a==nullptr)", false, false, Settings::Unspecified, "test.c")); + ASSERT_EQUALS("( ! a )", tokenizeAndStringify("(a==nullptr)", false, false, Settings::Unspecified, "test.cpp")); } void simplifyMulAndParens() {