Teach CheckMemoryLeak about "alloc ; loop alloc ;" kinds of patterns. (#861)

Add an optional extended description…
This commit is contained in:
Simon Martin 2017-01-15 22:16:23 +01:00 committed by PKEuS
parent c82d8a0d06
commit ae939b1385
2 changed files with 33 additions and 0 deletions

View File

@ -1971,6 +1971,10 @@ const Token *CheckMemoryLeakInFunction::findleak(const Token *tokens)
return result->tokAt(2);
}
if ((result = Token::findmatch(tokens, "alloc ; loop|while1 {| alloc ;")) != nullptr) {
return result->tokAt(3 + (result->strAt(3) == "{"));
}
if ((result = Token::findsimplematch(tokens, "; alloc ; if assign ;")) != nullptr) {
return result->tokAt(4);
}

View File

@ -192,6 +192,7 @@ private:
TEST_CASE(forwhile9);
TEST_CASE(forwhile10);
TEST_CASE(forwhile11);
TEST_CASE(forwhile12);
TEST_CASE(switch2);
TEST_CASE(switch3);
@ -1348,7 +1349,35 @@ private:
ASSERT_EQUALS("", errout.str());
}
void forwhile12() {
check("extern int bar();\n"
"void f() {\n"
" FILE *fp = fopen(\"name\", \"r\" );\n"
" while(bar()) {\n"
" fp = fopen(\"name\", \"w\");\n"
" fclose(fp);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Resource leak: fp\n", errout.str());
check("void f() {\n"
" FILE *fp = fopen(\"name\", \"r\" );\n"
" while(1) {\n"
" fp = fopen(\"name\", \"w\");\n"
" fclose(fp);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fp\n", errout.str());
check("void f() {\n"
" FILE *fp = fopen(\"name\", \"r\" );\n"
" for( ; ; ) {\n"
" fp = fopen(\"name\", \"w\");\n"
" fclose(fp);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fp\n", errout.str());
}
void switch2() {