Added support for delete and delete[] in invalidDeallocation check (fixes #1773)
This commit is contained in:
parent
dee5568853
commit
746beb98bf
|
@ -147,7 +147,9 @@ 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% ) ;") && 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);
|
errorInvalidDeallocation(tok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -315,13 +315,30 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void testinvaliddealloc() {
|
void testinvaliddealloc() {
|
||||||
check("int* func1()\n"
|
check("void func1() {\n"
|
||||||
"{\n"
|
" char tmp1[256];\n"
|
||||||
"int a;\n"
|
" free(tmp1);\n"
|
||||||
"char tmp[256];\n"
|
" char tmp2[256];\n"
|
||||||
"free (tmp);\n"
|
" delete tmp2;\n"
|
||||||
"}\n");
|
" char tmp3[256];\n"
|
||||||
ASSERT_EQUALS(std::string("[test.cpp:5]: (error) Deallocating auto-variable is invalid\n"), errout.str());
|
" 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"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
|
Loading…
Reference in New Issue