diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index a6c681814..c0f0830ad 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -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; }); diff --git a/test/cfg/posix.c b/test/cfg/posix.c index 90428bc3b..c360ab0db 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -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 diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 90d5d14ec..2d97e94bf 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -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()); }