Fixed #8392 (false positive: "Memory leak" with malloc in if)

This commit is contained in:
Daniel Marjamäki 2019-01-27 10:27:39 +01:00
parent 606ba4fc1a
commit 54cebfaf94
2 changed files with 23 additions and 1 deletions

View File

@ -404,7 +404,8 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
tokens.pop(); tokens.pop();
if (!tok3) if (!tok3)
continue; continue;
if (tok3->str() == "&&") { if (tok3->str() == "&&" || tok3->str() == "||") {
// FIXME: handle && ! || better
tokens.push(tok3->astOperand1()); tokens.push(tok3->astOperand1());
tokens.push(tok3->astOperand2()); tokens.push(tok3->astOperand2());
continue; continue;

View File

@ -101,6 +101,7 @@ private:
TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL)) TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL))
TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4))) TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4)))
TEST_CASE(ifelse12); // #8340 - if ((*p = malloc(4)) == NULL) TEST_CASE(ifelse12); // #8340 - if ((*p = malloc(4)) == NULL)
TEST_CASE(ifelse13); // #8392
// switch // switch
TEST_CASE(switch1); TEST_CASE(switch1);
@ -1181,6 +1182,26 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void ifelse13() { // #8392
check("int f(int fd, const char *mode) {\n"
" char *path;\n"
" if (fd == -1 || (path = (char *)malloc(10)) == NULL)\n"
" return 1;\n"
" free(path);\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int f(int fd, const char *mode) {\n"
" char *path;\n"
" if ((path = (char *)malloc(10)) == NULL || fd == -1)\n"
" return 1;\n" // <- memory leak
" free(path);\n"
" return 0;\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:4] memory leak", "", errout.str());
}
void switch1() { void switch1() {
check("void f() {\n" check("void f() {\n"
" char *p = 0;\n" " char *p = 0;\n"