Fixed #6506 (Properly detect calls to the deallocating free() function)

This commit is contained in:
Simon Martin 2015-06-20 21:00:54 +02:00 committed by Daniel Marjamäki
parent 1d49334398
commit 7481fbb028
2 changed files with 25 additions and 2 deletions

View File

@ -202,11 +202,13 @@ void CheckAutoVariables::autoVariables()
errorReturnAddressOfFunctionParameter(tok, tok->strAt(2)); errorReturnAddressOfFunctionParameter(tok, tok->strAt(2));
} }
// Invalid pointer deallocation // Invalid pointer deallocation
else if (Token::Match(tok, "free ( %var% ) ;") || (_tokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| %var% !!["))) { else if ((Token::Match(tok, "%name% ( %var% ) ;") && _settings->library.dealloc(tok)) ||
(_tokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| %var% !!["))) {
tok = Token::findmatch(tok->next(), "%var%"); tok = Token::findmatch(tok->next(), "%var%");
if (isAutoVarArray(tok)) if (isAutoVarArray(tok))
errorInvalidDeallocation(tok); errorInvalidDeallocation(tok);
} else if (Token::Match(tok, "free ( & %var% ) ;") || (_tokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) { } else if ((Token::Match(tok, "%name% ( & %var% ) ;") && _settings->library.dealloc(tok)) ||
(_tokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) {
tok = Token::findmatch(tok->next(), "%var%"); tok = Token::findmatch(tok->next(), "%var%");
if (isAutoVar(tok)) if (isAutoVar(tok))
errorInvalidDeallocation(tok); errorInvalidDeallocation(tok);

View File

@ -34,6 +34,7 @@ private:
errout.str(""); errout.str("");
Settings settings; Settings settings;
LOAD_LIB_2(settings.library, "std.cfg");
settings.inconclusive = inconclusive; settings.inconclusive = inconclusive;
settings.addEnabled("warning"); settings.addEnabled("warning");
settings.addEnabled("style"); settings.addEnabled("style");
@ -559,6 +560,26 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #6506
check("struct F {\n"
" void free(void*) {}\n"
"};\n"
"void foo() {\n"
" char c1[1];\n"
" F().free(c1);\n"
" char *c2 = 0;\n"
" F().free(&c2);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("class foo {\n"
" void free(void* );\n"
" void someMethod() {\n"
" char **dst_copy = NULL;\n"
" free(&dst_copy);\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
} }
void testinvaliddealloc_C() { void testinvaliddealloc_C() {