Fix #1733 FN Memory leak not detected when variable is 'used' by allocation function in condition (#3838)

This commit is contained in:
chrchr-github 2022-02-16 21:31:24 +01:00 committed by GitHub
parent b07814f329
commit a1adbff683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -1850,7 +1850,8 @@
<use-retval/>
<returnValue type="FILE *"/>
<noreturn>false</noreturn>
<arg nr="1" direction="in">
<leak-ignore/>
<arg nr="1" direction="in">
<not-null/>
<not-uninit/>
<strz/>

View File

@ -161,6 +161,7 @@ private:
TEST_CASE(ifelse21);
TEST_CASE(ifelse22); // #10187
TEST_CASE(ifelse23); // #5473
TEST_CASE(ifelse24); // #1733
// switch
TEST_CASE(switch1);
@ -1717,6 +1718,36 @@ private:
ASSERT_EQUALS("[test.c:2]: (error) Resource leak: fp\n", errout.str());
}
void ifelse24() { // #1733
Settings s;
LOAD_LIB_2(s.library, "std.cfg");
LOAD_LIB_2(s.library, "posix.cfg");
check("void f() {\n"
" char* temp = strdup(\"temp.txt\");\n"
" FILE* fp;\n"
" if (NULL == x || NULL == (fp = fopen(temp, \"rt\")))\n"
" return;\n"
"}\n", s);
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: temp\n"
"[test.cpp:6]: (error) Memory leak: temp\n"
"[test.cpp:6]: (error) Resource leak: fp\n",
errout.str());
check("FILE* f() {\n"
" char* temp = strdup(\"temp.txt\");\n"
" FILE* fp = fopen(temp, \"rt\");\n"
" return fp;\n"
"}\n", s);
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: temp\n", errout.str());
check("FILE* f() {\n"
" char* temp = strdup(\"temp.txt\");\n"
" return fopen(temp, \"rt\");\n"
"}\n", s);
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: temp\n", "", errout.str());
}
void switch1() {
check("void f() {\n"
" char *p = 0;\n"