Fixed #4405 (False positive: Memory in freed twice (throw))

This commit is contained in:
Zachary Blair 2013-01-05 19:02:19 -08:00
parent f5ebbff0a3
commit f3c3b7c910
2 changed files with 49 additions and 5 deletions

View File

@ -2716,8 +2716,6 @@ void CheckOther::checkDoubleFree()
std::set<unsigned int> closeDirVariables; std::set<unsigned int> closeDirVariables;
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
bool isUnknown = true;
// Keep track of any variables passed to "free()", "g_free()" or "closedir()", // Keep track of any variables passed to "free()", "g_free()" or "closedir()",
// and report an error if the same variable is passed twice. // and report an error if the same variable is passed twice.
if (Token::Match(tok, "free|g_free|closedir ( %var% )")) { if (Token::Match(tok, "free|g_free|closedir ( %var% )")) {
@ -2751,7 +2749,7 @@ void CheckOther::checkDoubleFree()
} }
// If this scope doesn't return, clear the set of previously freed variables // If this scope doesn't return, clear the set of previously freed variables
else if (tok->str() == "}" && _tokenizer->IsScopeNoReturn(tok, &isUnknown) && !isUnknown) { else if (tok->str() == "}" && _tokenizer->IsScopeNoReturn(tok)) {
freedVariables.clear(); freedVariables.clear();
closeDirVariables.clear(); closeDirVariables.clear();
} }

View File

@ -6222,7 +6222,7 @@ private:
" free(x);\n" " free(x);\n"
"}" "}"
); );
ASSERT_EQUALS("[test.cpp:8]: (error) Memory pointed to by 'x' is freed twice.\n", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:8]: (error) Memory pointed to by 'x' is freed twice.\n", "", errout.str());
check( check(
"void foo(int y)\n" "void foo(int y)\n"
@ -6265,7 +6265,53 @@ private:
" free(p);\n" " free(p);\n"
"}" "}"
); );
ASSERT_EQUALS("[test.cpp:8]: (error) Memory pointed to by 'p' is freed twice.\n", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:8]: (error) Memory pointed to by 'p' is freed twice.\n", "", errout.str());
check(
"void MyFuction()\n"
"{\n"
" char* data = new char[100];\n"
" try\n"
" {\n"
" }\n"
" catch(err)\n"
" {\n"
" delete[] data;\n"
" MyThrow(err);\n"
" }\n"
" delete[] data;\n"
"}\n"
"void MyThrow(err)\n"
"{\n"
" throw(err);\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check(
"void MyFuction()\n"
"{\n"
" char* data = new char[100];\n"
" try\n"
" {\n"
" }\n"
" catch(err)\n"
" {\n"
" delete[] data;\n"
" MyExit(err);\n"
" }\n"
" delete[] data;\n"
"}\n"
"void MyExit(err)\n"
"{\n"
" exit(err);\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
} }