#5691 False positive: autovarInvalidDeallocation - function name 'delete' in C code

This commit is contained in:
Alexander Mai 2014-05-01 07:32:37 +02:00
parent ab4f58a146
commit d2ebd718a9
2 changed files with 15 additions and 4 deletions

View File

@ -196,11 +196,11 @@ 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% ) ;") || Token::Match(tok, "delete [| ]| (| %var% !![")) { else if (Token::Match(tok, "free ( %var% ) ;") || (_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% ) ;") || Token::Match(tok, "delete [| ]| (| & %var% !![")) { } else if (Token::Match(tok, "free ( & %var% ) ;") || (_tokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) {
tok = Token::findmatch(tok->next(), "%var%"); tok = Token::findmatch(tok->next(), "%var%");
if (tok && tok->variable() && tok->variable()->isLocal()) if (tok && tok->variable() && tok->variable()->isLocal())
errorInvalidDeallocation(tok); errorInvalidDeallocation(tok);

View File

@ -34,7 +34,7 @@ private:
void check(const char code[], bool inconclusive=false, bool runSimpleChecks=true) { void check(const char code[], bool inconclusive=false, bool runSimpleChecks=true, const char* filename=nullptr) {
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
@ -45,7 +45,7 @@ private:
// Tokenize.. // Tokenize..
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, (filename)?filename:"test.cpp");
CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this); CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this);
checkAutoVariables.returnReference(); checkAutoVariables.returnReference();
@ -89,6 +89,7 @@ private:
TEST_CASE(testautovar_return4); // ticket #3030 TEST_CASE(testautovar_return4); // ticket #3030
TEST_CASE(testautovar_extern); TEST_CASE(testautovar_extern);
TEST_CASE(testinvaliddealloc); TEST_CASE(testinvaliddealloc);
TEST_CASE(testinvaliddealloc_C);
TEST_CASE(testassign1); // Ticket #1819 TEST_CASE(testassign1); // Ticket #1819
TEST_CASE(testassign2); // Ticket #2765 TEST_CASE(testassign2); // Ticket #2765
@ -513,6 +514,16 @@ private:
" delete[] pKoeff;\n" " delete[] pKoeff;\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Deallocation of an auto-variable results in undefined behaviour.\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Deallocation of an auto-variable results in undefined behaviour.\n", "", errout.str());
}
void testinvaliddealloc_C() {
// #5691
check("void svn_repos_dir_delta2() {\n"
" struct context c;\n"
" SVN_ERR(delete(&c, root_baton, src_entry, pool));\n"
"}\n", false, true, "test.c");
ASSERT_EQUALS("", errout.str());
} }
void testassign1() { // Ticket #1819 void testassign1() { // Ticket #1819