Fix #8327 (Memleak with mmap return value check) (#2864)

Also fix a broken test case related to checking the return value -1
where socket was not defined in the library used in the tests.

This was tested running test-my-pr with 500 packages. The difference was
six fewer FPs.
This commit is contained in:
Rikard Falkeborn 2020-10-29 13:17:33 +01:00 committed by GitHub
parent 16f4f198eb
commit de19dc9e3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -510,6 +510,8 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
varInfo2.erase(vartok->varId());
} else if (astIsVariableComparison(tok3, "==", "-1", &vartok)) {
varInfo1.erase(vartok->varId());
} else if (astIsVariableComparison(tok3, "!=", "-1", &vartok)) {
varInfo2.erase(vartok->varId());
}
return ChildrenToVisit::none;
});

View File

@ -200,6 +200,14 @@ void memleak_mmap(int fd)
// cppcheck-suppress memleak
}
void * memleak_mmap2() // #8327
{
void * data = mmap(NULL, 10, PROT_READ, MAP_PRIVATE, 1, 0);
if (data != MAP_FAILED)
return data;
return NULL;
}
void resourceLeak_fdopen(int fd)
{
// cppcheck-suppress unreadVariable

View File

@ -41,6 +41,9 @@ private:
settings.library.setalloc("malloc", id, -1);
settings.library.setrealloc("realloc", id, -1);
settings.library.setdealloc("free", id, 1);
while (!Library::ismemory(++id));
settings.library.setalloc("socket", id, -1);
settings.library.setdealloc("close", id, 1);
while (!Library::isresource(++id));
settings.library.setalloc("fopen", id, -1);
settings.library.setrealloc("freopen", id, -1, 3);
@ -1392,10 +1395,19 @@ private:
}
void ifelse8() { // #5747
check("void f() {\n"
check("int f() {\n"
" int fd = socket(AF_INET, SOCK_PACKET, 0 );\n"
" if (fd == -1)\n"
" return;\n"
" return -1;\n"
" return fd;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f() {\n"
" int fd = socket(AF_INET, SOCK_PACKET, 0 );\n"
" if (fd != -1)\n"
" return fd;\n"
" return -1;\n"
"}");
ASSERT_EQUALS("", errout.str());
}