Fixed #8340 (incorrect memleak errors)

This commit is contained in:
Daniel Marjamäki 2019-01-09 19:50:41 +01:00
parent 5051744304
commit 75e367c48b
2 changed files with 10 additions and 1 deletions

View File

@ -364,7 +364,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
for (const Token *innerTok = tok->tokAt(2); innerTok && innerTok != closingParenthesis; innerTok = innerTok->next()) {
// TODO: replace with checkTokenInsideExpression()
if (Token::Match(innerTok, "%var% =")) {
if (Token::Match(innerTok, "%var% =") && innerTok->astParent() == innerTok->next()) {
// allocation?
if (Token::Match(innerTok->tokAt(2), "%type% (")) {
const Library::AllocFunc* f = mSettings->library.alloc(innerTok->tokAt(2));

View File

@ -100,6 +100,7 @@ private:
TEST_CASE(ifelse9); // #5273 - if (X(p==NULL, 0))
TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL))
TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4)))
TEST_CASE(ifelse12); // #8340 - if ((*p = malloc(4)) == NULL)
// switch
TEST_CASE(switch1);
@ -1172,6 +1173,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
void ifelse12() { // #8340
check("void f(char **p) {\n"
" if ((*p = malloc(4)) == NULL)\n"
" return;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void switch1() {
check("void f() {\n"
" char *p = 0;\n"