Skip to content

Commit afc317e

Browse files
Partial fix for #14524 FN memleak with C++ cast (danmar#8249)
1 parent 15424d0 commit afc317e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/checkmemoryleak.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
5656
{
5757
// What we may have...
5858
// * var = (char *)malloc(10);
59+
// * var = static_cast<char *>(malloc(10));
5960
// * var = new char[10];
6061
// * var = strdup("hello");
6162
// * var = strndup("hello", 3);
6263
if (tok2 && tok2->str() == "(") {
6364
tok2 = tok2->link();
6465
tok2 = tok2 ? tok2->next() : nullptr;
6566
}
67+
if (tok2 && tok2->isCpp() && tok2->isKeyword() && endsWith(tok2->str(), "_cast"))
68+
tok2 = tok2->astParent()->next();
6669
if (!tok2)
6770
return No;
6871
if (tok2->str() == "::")

test/testmemleak.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ class TestMemleakStructMember : public TestFixture {
16741674
TEST_CASE(assign3);
16751675
TEST_CASE(assign4); // #11019
16761676
TEST_CASE(assign5);
1677+
TEST_CASE(assign6);
16771678

16781679
// Failed allocation
16791680
TEST_CASE(failedAllocation);
@@ -1955,6 +1956,15 @@ class TestMemleakStructMember : public TestFixture {
19551956
ASSERT_EQUALS("", errout_str());
19561957
}
19571958

1959+
void assign6() {
1960+
check("struct S { S* p; };\n" // #14524
1961+
"void f() {\n"
1962+
" S s;\n"
1963+
" s.p = static_cast<S*>(malloc(sizeof(S)));\n"
1964+
"}\n");
1965+
ASSERT_EQUALS("[test.cpp:5:1]: (error) Memory leak: s.p [memleak]\n", errout_str());
1966+
}
1967+
19581968
void failedAllocation() {
19591969
check("static struct ABC * foo()\n"
19601970
"{\n"

0 commit comments

Comments
 (0)