Fixed #1416 (false negative resource leak when calling fdopen)

This commit is contained in:
Daniel Marjamäki 2014-04-20 10:50:32 +02:00
parent 36f4431481
commit 44a66e6a5a
3 changed files with 38 additions and 32 deletions

View File

@ -8,31 +8,31 @@
<function name="mkdtemp"> <noreturn>false</noreturn> <arg nr="1"><not-uninit/><not-null/></arg> <leak-ignore/> </function>
<function name="mktemp"> <noreturn>false</noreturn> <arg nr="1"><not-uninit/><not-null/></arg> <leak-ignore/> </function>
<function name="getcwd">
<noreturn>false</noreturn>
<arg nr="1"><not-null/></arg>
<arg nr="2"><not-uninit/><not-null/></arg>
<leak-ignore/>
</function>
<noreturn>false</noreturn>
<arg nr="1"><not-null/></arg>
<arg nr="2"><not-uninit/><not-null/></arg>
<leak-ignore/>
</function>
<function name="mkdir">
<noreturn>false</noreturn>
<arg nr="1"><not-uninit/><not-null/></arg>
<arg nr="2"><not-uninit/></arg>
<leak-ignore/>
</function>
<noreturn>false</noreturn>
<arg nr="1"><not-uninit/><not-null/></arg>
<arg nr="2"><not-uninit/></arg>
<leak-ignore/>
</function>
<function name="rmdir"> <noreturn>false</noreturn> <arg nr="1"><not-uninit/><not-null/></arg> <leak-ignore/> </function>
<function name="chdir"> <noreturn>false</noreturn> <arg nr="1"><not-uninit/><not-null/></arg> <leak-ignore/> </function>
<function name="link">
<noreturn>false</noreturn>
<arg nr="1"><not-uninit/><not-null/></arg>
<arg nr="2"><not-uninit/><not-null/></arg>
<leak-ignore/>
</function>
<noreturn>false</noreturn>
<arg nr="1"><not-uninit/><not-null/></arg>
<arg nr="2"><not-uninit/><not-null/></arg>
<leak-ignore/>
</function>
<function name="rename"> <noreturn>false</noreturn> <arg nr="1"><not-uninit/><not-null/></arg> <leak-ignore/> </function>
<function name="isatty"> <noreturn>false</noreturn> <arg nr="1"><not-uninit/></arg> <leak-ignore/> </function>
<function name="popen"><noreturn>false</noreturn><arg nr="1"><not-null/><not-uninit/></arg><arg nr="2"><not-null/><not-uninit/></arg></function>
<function name="pclose"><noreturn>false</noreturn><arg nr="1"><not-null/><not-uninit/></arg></function>
<function name="fdopen"><noreturn>false</noreturn><arg nr="1"><not-null/><not-uninit/></arg></function>
<resource>
<dealloc>close</dealloc>
@ -46,9 +46,13 @@
<alloc init="true">fdopendir</alloc>
</resource>
<resource>
<dealloc>fclose</dealloc>
<alloc init="true">fdopen</alloc>
</resource>
<resource>
<dealloc>pclose</dealloc>
<alloc init="true">popen</alloc>
</resource>
</def>

View File

@ -184,6 +184,8 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
if (alloctype > 0) {
if (alloctype == settings1->library.dealloc("free"))
return Malloc;
if (alloctype == settings1->library.dealloc("fclose"))
return File;
return Library::ismemory(alloctype) ? OtherMem : OtherRes;
}
}
@ -612,7 +614,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
if (noreturn.find(tok->str()) != noreturn.end() && tok->strAt(-1) != "=")
return "exit";
if (varid > 0 && (getAllocationType(tok, varid) != No || getReallocationType(tok, varid) != No || getDeallocationType(tok, varid) != No))
if (varid > 0 && (getReallocationType(tok, varid) != No || getDeallocationType(tok, varid) != No))
return 0;
if (callstack.size() > 2)

View File

@ -330,7 +330,6 @@ private:
TEST_CASE(getc_function);
TEST_CASE(open_function);
TEST_CASE(open_fdopen);
TEST_CASE(creat_function);
TEST_CASE(close_function);
TEST_CASE(fd_functions);
@ -3876,19 +3875,6 @@ private:
ASSERT_EQUALS("", errout.str());
}
void open_fdopen() {
// Ticket #2830
Settings settings;
settings.standards.posix = true;
check("void f(const char *path)\n"
"{\n"
" int fd = open(path, O_RDONLY);\n"
" FILE *f = fdopen(fd, x);\n"
" fclose(f);\n"
"}", &settings);
ASSERT_EQUALS("", errout.str());
}
void creat_function() {
Settings settings;
settings.standards.posix = true;
@ -4267,6 +4253,20 @@ private:
check(code2, &settings);
ASSERT_EQUALS("", errout.str());
// Ticket #2830
check("void f(const char *path) {\n"
" int fd = open(path, O_RDONLY);\n"
" FILE *f = fdopen(fd, x);\n"
" fclose(f);\n"
"}", &settings);
ASSERT_EQUALS("", errout.str());
// Ticket #1416
check("void f(void) {\n"
" FILE *f = fdopen(0, \"r\");\n"
"}", &settings);
ASSERT_EQUALS("[test.cpp:3]: (error) Resource leak: f\n", errout.str());
LOAD_LIB_2(settings.library, "gtk.cfg");
check("void f(char *a) {\n"