Fixed #3954 (Pointer reference memory leak false positive)

This commit is contained in:
Daniel Marjamäki 2012-07-17 16:28:34 +02:00
parent d34924ba6d
commit a768b0e8b2
4 changed files with 22 additions and 1 deletions

View File

@ -311,6 +311,10 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
continue; continue;
} }
// Don't check reference variables
if (var && var->isReference())
continue;
// allocation? // allocation?
if (Token::Match(tok->tokAt(2), "%type% (")) { if (Token::Match(tok->tokAt(2), "%type% (")) {
const std::map<std::string, std::string>::const_iterator it = allocFunctions.find(tok->strAt(2)); const std::map<std::string, std::string>::const_iterator it = allocFunctions.find(tok->strAt(2));

View File

@ -2237,7 +2237,7 @@ void CheckMemoryLeakInFunction::check()
if (!var || (!var->isLocal() && !var->isArgument()) || var->isStatic() || !var->scope()) if (!var || (!var->isLocal() && !var->isArgument()) || var->isStatic() || !var->scope())
continue; continue;
if (var->isArgument() && var->isReference()) if (var->isReference())
continue; continue;
if (!var->isPointer() && var->typeStartToken()->str() != "int") if (!var->isPointer() && var->typeStartToken()->str() != "int")

View File

@ -85,6 +85,7 @@ private:
// General tests: variable type, allocation type, etc // General tests: variable type, allocation type, etc
TEST_CASE(test1); TEST_CASE(test1);
TEST_CASE(test2); TEST_CASE(test2);
TEST_CASE(test3); // #3954 - reference pointer
// Possible leak => Further configuration is needed for complete analysis // Possible leak => Further configuration is needed for complete analysis
TEST_CASE(configuration1); TEST_CASE(configuration1);
@ -471,6 +472,14 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void test3() { // 3954 - reference pointer
check("void f() {\n"
" char *&p = x();\n"
" p = malloc(10);\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void configuration1() { void configuration1() {
// Possible leak => configuration is required for complete analysis // Possible leak => configuration is required for complete analysis
// The user should be able to "white list" and "black list" functions. // The user should be able to "white list" and "black list" functions.

View File

@ -163,6 +163,7 @@ private:
TEST_CASE(staticvar); TEST_CASE(staticvar);
TEST_CASE(externvar); TEST_CASE(externvar);
TEST_CASE(referencevar); // 3954 - false positive for reference pointer
TEST_CASE(alloc_alloc_1); TEST_CASE(alloc_alloc_1);
@ -1005,6 +1006,13 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void referencevar() { // 3954 - false positive for reference pointer
check("void f() {\n"
" char *&x = get();\n"
" x = malloc(100);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void alloc_alloc_1() { void alloc_alloc_1() {