Fixed #7845 (Leak reported when ignoring return value of 'new', even if pointer saved by constructor)

This commit is contained in:
Daniel Marjamäki 2019-02-03 12:15:05 +01:00
parent ae001d4336
commit a4406aca32
2 changed files with 22 additions and 0 deletions

View File

@ -142,6 +142,14 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
return No;
if (tok2->astOperand1() && (tok2->astOperand1()->str() == "[" || (tok2->astOperand1()->astOperand1() && tok2->astOperand1()->astOperand1()->str() == "[")))
return NewArray;
const Token *typeTok = tok2->next();
while (Token::Match(typeTok, "%name% :: %name%"))
typeTok = typeTok->tokAt(2);
if (typeTok->type() && typeTok->type()->isClassType()) {
const Scope *classScope = typeTok->type()->classScope;
if (classScope && classScope->numConstructors > 0)
return No;
}
return New;
}

View File

@ -1804,6 +1804,9 @@ private:
// pass allocated memory to function using a smart pointer
TEST_CASE(smartPointerFunctionParam);
TEST_CASE(resourceLeak);
// Test getAllocationType for subfunction
TEST_CASE(getAllocationType);
}
void functionParameter() {
@ -2087,6 +2090,17 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
}
void getAllocationType() {
// #7845
check("class Thing { Thing(); };\n"
"Thing * makeThing() { Thing *thing = new Thing; return thing; }\n"
"\n"
"void f() {\n"
" makeThing();\n"
"}");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestMemleakNoVar)