Added support for delete and delete[] in invalidDeallocation check (fixes #1773)

This commit is contained in:
PKEuS 2012-04-26 16:44:33 +02:00
parent dee5568853
commit 746beb98bf
2 changed files with 28 additions and 9 deletions

View File

@ -147,7 +147,9 @@ void CheckAutoVariables::autoVariables()
errorReturnAddressOfFunctionParameter(tok, tok->strAt(2));
}
// Invalid pointer deallocation
else if (Token::Match(tok, "free ( %var% ) ;") && isAutoVarArray(tok->tokAt(2)->varId())) {
else if (Token::Match(tok, "free ( %var% ) ;") || Token::Match(tok, "delete [| ]| (| %var% !![")) {
tok = Token::findmatch(tok->next(), "%var%");
if (isAutoVarArray(tok->varId()))
errorInvalidDeallocation(tok);
}
}

View File

@ -315,13 +315,30 @@ private:
}
void testinvaliddealloc() {
check("int* func1()\n"
"{\n"
"int a;\n"
"char tmp[256];\n"
"free (tmp);\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:5]: (error) Deallocating auto-variable is invalid\n"), errout.str());
check("void func1() {\n"
" char tmp1[256];\n"
" free(tmp1);\n"
" char tmp2[256];\n"
" delete tmp2;\n"
" char tmp3[256];\n"
" delete (tmp3);\n"
" char tmp4[256];\n"
" delete[] (tmp4);\n"
" char tmp5[256];\n"
" delete[] tmp5;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Deallocating auto-variable is invalid\n"
"[test.cpp:5]: (error) Deallocating auto-variable is invalid\n"
"[test.cpp:7]: (error) Deallocating auto-variable is invalid\n"
"[test.cpp:9]: (error) Deallocating auto-variable is invalid\n"
"[test.cpp:11]: (error) Deallocating auto-variable is invalid\n", errout.str());
check("void func1() {\n"
" char* tmp1[256];\n"
" init(tmp1);\n"
" delete tmp1[34];\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"