Fixed #4035 (False positive: Memory leak: pTempFile)

This commit is contained in:
Daniel Marjamäki 2012-08-25 12:00:25 +02:00
parent 730935efdd
commit 985ac662ee
4 changed files with 30 additions and 34 deletions

View File

@ -1995,8 +1995,6 @@ bool Tokenizer::tokenize(std::istream &code,
removeRedundantSemicolons(); removeRedundantSemicolons();
simplifyReservedWordNullptr();
simplifyParameterVoid(); simplifyParameterVoid();
simplifyRedundantConsecutiveBraces(); simplifyRedundantConsecutiveBraces();
@ -2106,6 +2104,14 @@ void Tokenizer::simplifyNull()
tok->str("0"); 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() 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() void Tokenizer::simplifyRedundantConsecutiveBraces()
{ {
// Remove redundant consecutive braces, i.e. '.. { { .. } } ..' -> '.. { .. } ..'. // Remove redundant consecutive braces, i.e. '.. { { .. } } ..' -> '.. { .. } ..'.

View File

@ -463,8 +463,6 @@ public:
bool hasComplicatedSyntaxErrorsInTemplates(); bool hasComplicatedSyntaxErrorsInTemplates();
void simplifyReservedWordNullptr();
/** /**
* Simplify e.g. 'atol("0")' into '0' * Simplify e.g. 'atol("0")' into '0'
*/ */

View File

@ -74,19 +74,18 @@ private:
TEST_CASE(crash1); 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.. // Clear the error buffer..
errout.str(""); errout.str("");
Settings settings; Settings settings;
settings.addEnabled("style"); settings.addEnabled("style");
settings.inconclusive = inconclusive; settings.inconclusive = inconclusive;
settings.standards.cpp11 = cpp11;
// Tokenize.. // Tokenize..
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
if (!tokenizer.tokenize(istr, "test.cpp")) if (!tokenizer.tokenize(istr, filename))
return; return;
// Check for redundant code.. // Check for redundant code..
@ -1159,19 +1158,17 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", errout.str());
} }
void nullpointer12() { // ticket #2470 void nullpointer12() { // ticket #2470, #4035
check("int foo()\n" const char code[] = "int foo()\n"
"{\n" "{\n"
" int* i = nullptr;\n" " int* i = nullptr;\n"
" return *i;\n" " return *i;\n"
"}\n", false, true); // Check as C++11 code "}\n";
check(code, false, "test.cpp"); // C++ file => nullptr means NULL
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str());
check("int foo(int nullptr)\n" check(code, false, "test.c"); // C file => nullptr does not mean NULL
"{\n"
" int* i = nullptr;\n"
" return *i;\n"
"}", true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }

View File

@ -4646,12 +4646,17 @@ private:
} }
void simplify_null() { void simplify_null() {
const char code[] = {
"int * p = NULL;\n" const char code[] =
"int * q = __null;\n"; "int * p = NULL;\n"
const char expected[] = "int * q = __null;\n";
"int * p ; p = 0 ;\nint * q ; q = 0 ;"; const char expected[] =
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); "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() { void simplifyMulAndParens() {