Fixed #7244 (False memory leak when POD is allocated with value initialization and pushed onto container)

This commit is contained in:
Daniel Marjamäki 2016-05-14 14:56:51 +02:00
parent 251fc022fa
commit 5a1bea2a09
2 changed files with 14 additions and 1 deletions

View File

@ -829,6 +829,16 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
}
}
if (Token::simpleMatch(tok->next(), "= new")) {
tok = tok->tokAt(2);
while (Token::Match(tok->next(), "%name%|::|(|[")) {
if (Token::Match(tok->next(), "(|["))
tok = tok->linkAt(1);
else
tok = tok->next();
}
}
if (alloc == No && alloctype == No)
alloctype = CheckMemoryLeak::New;
}

View File

@ -413,6 +413,10 @@ private:
ASSERT_EQUALS(";;assign;", getcode("A * a = new (X) A;", "a"));
ASSERT_EQUALS(";;alloc;", getcode("int i = open(a,b);", "i"));
ASSERT_EQUALS(";;assign;", getcode("int i = open();", "i"));
ASSERT_EQUALS(";;alloc;use;", getcode("int *p; dostuff(p = new int);", "p"));
ASSERT_EQUALS(";;alloc;use;", getcode("int *p; dostuff(p = new int());", "p"));
ASSERT_EQUALS(";;alloc;use;", getcode("int *p; fred.dostuff(p = new int);", "p"));
ASSERT_EQUALS(";;alloc;use;", getcode("int *p; fred.dostuff(p = new int());", "p"));
// alloc; return use;
ASSERT_EQUALS(";;alloc;returnuse;", getcode("int *a = new int[10]; return a;", "a"));
@ -509,7 +513,6 @@ private:
ASSERT_EQUALS(";;;use;", getcode("char *p; const char *q; q = p;", "p"));
ASSERT_EQUALS(";;use;;", getcode("char *s; x = {1,s};", "s"));
ASSERT_EQUALS(";{};;alloc;;use;", getcode("struct Foo { }; Foo *p; p = malloc(10); const Foo *q; q = p;", "p"));
ASSERT_EQUALS(";;alloc;use;", getcode("Fred *fred; p.setFred(fred = new Fred);", "fred"));
ASSERT_EQUALS(";;useuse_;", getcode("struct AB *ab; f(ab->a);", "ab"));
ASSERT_EQUALS(";;use;", getcode("struct AB *ab; ab = pop(ab);", "ab"));