Fix FP with cast pointer to free() (#1961)

This fixes false positives when the pointer passed to free() (or similar
deallocation functions) is cast using a c-style cast.
This commit is contained in:
Rikard Falkeborn 2019-07-10 09:13:59 +02:00 committed by Daniel Marjamäki
parent 7ac22677b8
commit a1a14b8465
2 changed files with 18 additions and 0 deletions

View File

@ -778,6 +778,9 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
arg = arg->tokAt(5);
}
// Skip casts
while (arg && arg->isCast())
arg = arg->astOperand2() ? arg->astOperand2() : arg->astOperand1();
const Token * const argTypeStartTok = arg;
while (Token::Match(arg, "%name% .|:: %name%"))

View File

@ -64,6 +64,7 @@ private:
TEST_CASE(assign16);
TEST_CASE(assign17); // #9047
TEST_CASE(assign18);
TEST_CASE(assign19);
TEST_CASE(realloc1);
TEST_CASE(realloc2);
@ -358,6 +359,14 @@ private:
ASSERT_EQUALS("[test.c:3]: (error) Memory leak: p\n", errout.str());
}
void assign19() {
check("void f() {\n"
" char *p = malloc(10);\n"
" free((void*)p);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void realloc1() {
check("void f() {\n"
" void *p = malloc(10);\n"
@ -1345,6 +1354,12 @@ private:
"}");
ASSERT_EQUALS("[test.c:3]: (error) Mismatching allocation and deallocation: f\n", errout.str());
check("void f() {\n"
" FILE*f=fopen(fname,a);\n"
" free((void*)f);\n"
"}");
ASSERT_EQUALS("[test.c:3]: (error) Mismatching allocation and deallocation: f\n", errout.str());
check("void f() {\n"
" char *cPtr = new char[100];\n"
" delete[] cPtr;\n"