Skip to content

Commit

Permalink
Fix #12890 FP mismatchAllocDealloc with pointer to pointer (#6598)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jul 17, 2024
1 parent 1da0e0c commit 3cb392d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
6 changes: 4 additions & 2 deletions lib/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
return No;
if (tok2->str() == "::")
tok2 = tok2->next();
while (Token::Match(tok2, "%name% :: %type%"))
tok2 = tok2->tokAt(2);
if (!tok2->isName())
return No;

if (!Token::Match(tok2, "%name% ::|. %type%")) {
if (!Token::Match(tok2, "%name% . %type%")) {
// Using realloc..
AllocType reallocType = getReallocationType(tok2, varid);
if (reallocType != No)
Expand Down Expand Up @@ -124,7 +126,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
}
}

while (Token::Match(tok2,"%name% ::|. %type%"))
while (Token::Match(tok2,"%name% . %type%"))
tok2 = tok2->tokAt(2);

// User function
Expand Down
42 changes: 17 additions & 25 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,7 @@ class TestLeakAutoVar : public TestFixture {
Settings settings;

void run() override {
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <podtype name=\"uint8_t\" sign=\"u\" size=\"1\"/>\n"
" <memory>\n"
" <alloc>malloc</alloc>\n"
" <realloc>realloc</realloc>\n"
" <dealloc>free</dealloc>\n"
" </memory>\n"
" <resource>\n"
" <alloc>socket</alloc>\n"
" <dealloc>close</dealloc>\n"
" </resource>\n"
" <resource>\n"
" <alloc>fopen</alloc>\n"
" <realloc realloc-arg=\"3\">freopen</realloc>\n"
" <dealloc>fclose</dealloc>\n"
" </resource>\n"
" <smart-pointer class-name=\"std::shared_ptr\"/>\n"
" <smart-pointer class-name=\"std::unique_ptr\">\n"
" <unique/>\n"
" </smart-pointer>\n"
"</def>";
settings = settingsBuilder(settings).libraryxml(xmldata, sizeof(xmldata)).build();
settings = settingsBuilder(settings).library("std.cfg").build();

// Assign
TEST_CASE(assign1);
Expand Down Expand Up @@ -462,9 +440,10 @@ class TestLeakAutoVar : public TestFixture {
}

void assign22() { // #9139
const Settings s = settingsBuilder().library("posix.cfg").build();
check("void f(char tempFileName[256]) {\n"
" const int fd = socket(AF_INET, SOCK_PACKET, 0 );\n"
"}", true);
"}", true, &s);
ASSERT_EQUALS("[test.cpp:3]: (error) Resource leak: fd\n", errout_str());

check("void f() {\n"
Expand Down Expand Up @@ -622,6 +601,19 @@ class TestLeakAutoVar : public TestFixture {
" delete[] li.front().m_p;\n"
"}\n", true);
ASSERT_EQUALS("", errout_str());

check("struct S {\n" // #12890
" int** p;\n"
" S() {\n"
" p = std::malloc(sizeof(int*));\n"
" p[0] = new int;\n"
" }\n"
" ~S() {\n"
" delete p[0];\n"
" std::free(p);\n"
" }\n"
"};\n", true);
ASSERT_EQUALS("", errout_str());
}

void assign26() {
Expand Down Expand Up @@ -652,7 +644,7 @@ class TestLeakAutoVar : public TestFixture {
check("void f() {\n"
" std::string *str = new std::string;"
"}", true);
TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Memory leak: str\n", "", errout_str());
ASSERT_EQUALS("[test.cpp:2]: (error) Memory leak: str\n", errout_str());

check("class TestType {\n" // #9028
"public:\n"
Expand Down

0 comments on commit 3cb392d

Please sign in to comment.