Fix #11786 False positive: memory leak (#5182)

This commit is contained in:
chrchr-github 2023-06-22 23:03:55 +02:00 committed by GitHub
parent fe56b0c42a
commit b80460f3ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -609,7 +609,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
// unknown control.. (TODO: handle loops)
else if ((Token::Match(tok, "%type% (") && Token::simpleMatch(tok->linkAt(1), ") {")) || Token::simpleMatch(tok, "do {")) {
varInfo.clear();
break;
return false;
}
// return

View File

@ -178,6 +178,7 @@ private:
// loops
TEST_CASE(loop1);
TEST_CASE(loop2);
// mismatching allocation/deallocation
TEST_CASE(mismatchAllocDealloc);
@ -2070,6 +2071,17 @@ private:
ASSERT_EQUALS("", errout.str());
}
void loop2() {
check("void f() {\n" // #11786
" int* p = (int*)malloc(sizeof(int));\n"
" if (1) {\n"
" while (0) {}\n"
" free(p);\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void mismatchAllocDealloc() {
check("void f() {\n"
" FILE*f=fopen(fname,a);\n"