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:
parent
7ac22677b8
commit
a1a14b8465
|
@ -778,6 +778,9 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
|
||||||
arg = arg->tokAt(5);
|
arg = arg->tokAt(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip casts
|
||||||
|
while (arg && arg->isCast())
|
||||||
|
arg = arg->astOperand2() ? arg->astOperand2() : arg->astOperand1();
|
||||||
const Token * const argTypeStartTok = arg;
|
const Token * const argTypeStartTok = arg;
|
||||||
|
|
||||||
while (Token::Match(arg, "%name% .|:: %name%"))
|
while (Token::Match(arg, "%name% .|:: %name%"))
|
||||||
|
|
|
@ -64,6 +64,7 @@ private:
|
||||||
TEST_CASE(assign16);
|
TEST_CASE(assign16);
|
||||||
TEST_CASE(assign17); // #9047
|
TEST_CASE(assign17); // #9047
|
||||||
TEST_CASE(assign18);
|
TEST_CASE(assign18);
|
||||||
|
TEST_CASE(assign19);
|
||||||
|
|
||||||
TEST_CASE(realloc1);
|
TEST_CASE(realloc1);
|
||||||
TEST_CASE(realloc2);
|
TEST_CASE(realloc2);
|
||||||
|
@ -358,6 +359,14 @@ private:
|
||||||
ASSERT_EQUALS("[test.c:3]: (error) Memory leak: p\n", errout.str());
|
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() {
|
void realloc1() {
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" void *p = malloc(10);\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());
|
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"
|
check("void f() {\n"
|
||||||
" char *cPtr = new char[100];\n"
|
" char *cPtr = new char[100];\n"
|
||||||
" delete[] cPtr;\n"
|
" delete[] cPtr;\n"
|
||||||
|
|
Loading…
Reference in New Issue