Fixed #3660 (False positive memleak (allocation function uses non-local variable))

This commit is contained in:
Daniel Marjamäki 2012-06-25 20:00:50 +02:00
parent f5c42660de
commit 0042ee7bc8
2 changed files with 22 additions and 0 deletions

View File

@ -457,6 +457,15 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok,
if (varid == 0)
return No;
if (this != NULL) {
// If variable is not local then alloctype shall be "No"
// Todo: there can be false negatives about mismatching allocation/deallocation.
// => Generate "alloc ; use ;" if variable is not local?
const Variable *var = tokenizer->getSymbolDatabase()->getVariableFromVarId(varid);
if (!var || !var->isLocal() || var->isStatic())
return No;
}
// Check if return pointer is allocated..
AllocType allocType = No;
while (NULL != (tok = tok->next())) {

View File

@ -237,6 +237,7 @@ private:
TEST_CASE(allocfunc9);
TEST_CASE(allocfunc10);
TEST_CASE(allocfunc11);
TEST_CASE(allocfunc12); // #3660: allocating and returning non-local pointer => not allocfunc
TEST_CASE(throw1);
TEST_CASE(throw2);
@ -2521,6 +2522,18 @@ private:
ASSERT_EQUALS("", errout.str());
}
void allocfunc12() { // #3660: allocating and returning non-local pointer => not allocfunc
check("char *p;\n" // global pointer
"char *a() {\n"
" if (!p) p = malloc(10);\n"
" return p;\n"
"}\n"
"void b() {\n"
" char *x = a();\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void throw1() {
check("void foo()\n"
"{\n"