Skip to content

Commit 853a1f6

Browse files
authored
Fix 10631: FP, Regression: error: Return value of allocation function 'makeThing' is not stored. (#3585)
1 parent c26e205 commit 853a1f6

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,14 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
103103
const Token *typeTok = tok2->next();
104104
while (Token::Match(typeTok, "%name% :: %name%"))
105105
typeTok = typeTok->tokAt(2);
106+
const Scope* classScope = nullptr;
106107
if (typeTok->type() && typeTok->type()->isClassType()) {
107-
const Scope *classScope = typeTok->type()->classScope;
108-
if (classScope && classScope->numConstructors > 0)
109-
return No;
108+
classScope = typeTok->type()->classScope;
109+
} else if (typeTok->function() && typeTok->function()->isConstructor()) {
110+
classScope = typeTok->function()->nestedIn;
110111
}
112+
if (classScope && classScope->numConstructors > 0)
113+
return No;
111114
return New;
112115
}
113116

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
6969
createSymbolDatabaseSetVariablePointers();
7070
setValueTypeInTokenList(false);
7171
createSymbolDatabaseSetTypePointers();
72-
createSymbolDatabaseSetSmartPointerType();
7372
createSymbolDatabaseSetFunctionPointers(true);
73+
createSymbolDatabaseSetSmartPointerType();
7474
setValueTypeInTokenList(false);
7575
createSymbolDatabaseEnums();
7676
createSymbolDatabaseEscapeFunctions();

test/testmemleak.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,8 +1360,10 @@ class TestMemleakInClass : public TestFixture {
13601360
"public:\n"
13611361
" A() : b(new B()), c(new C(b)) { }\n"
13621362
"}");
1363-
ASSERT_EQUALS("[test.cpp:9]: (style) Class 'A' is unsafe, 'A::b' can leak by wrong usage.\n"
1364-
"[test.cpp:10]: (style) Class 'A' is unsafe, 'A::c' can leak by wrong usage.\n", errout.str());
1363+
TODO_ASSERT_EQUALS("[test.cpp:9]: (style) Class 'A' is unsafe, 'A::b' can leak by wrong usage.\n"
1364+
"[test.cpp:10]: (style) Class 'A' is unsafe, 'A::c' can leak by wrong usage.\n",
1365+
"[test.cpp:9]: (style) Class 'A' is unsafe, 'A::b' can leak by wrong usage.\n",
1366+
errout.str());
13651367

13661368
check("struct B { };\n"
13671369
"struct C\n"
@@ -1380,8 +1382,10 @@ class TestMemleakInClass : public TestFixture {
13801382
" c = new C(b);\n"
13811383
" }\n"
13821384
"}");
1383-
ASSERT_EQUALS("[test.cpp:9]: (style) Class 'A' is unsafe, 'A::b' can leak by wrong usage.\n"
1384-
"[test.cpp:10]: (style) Class 'A' is unsafe, 'A::c' can leak by wrong usage.\n", errout.str());
1385+
TODO_ASSERT_EQUALS("[test.cpp:9]: (style) Class 'A' is unsafe, 'A::b' can leak by wrong usage.\n"
1386+
"[test.cpp:10]: (style) Class 'A' is unsafe, 'A::c' can leak by wrong usage.\n",
1387+
"[test.cpp:9]: (style) Class 'A' is unsafe, 'A::b' can leak by wrong usage.\n",
1388+
errout.str());
13851389
}
13861390

13871391
void class22() { // ticket #3012 - false positive
@@ -2521,6 +2525,26 @@ class TestMemleakNoVar : public TestFixture {
25212525
" makeThing();\n"
25222526
"}");
25232527
ASSERT_EQUALS("", errout.str());
2528+
2529+
// #10631
2530+
check("struct Thing {\n"
2531+
" Thing();\n"
2532+
"};\n"
2533+
"std::vector<Thing*> g_things;\n"
2534+
"Thing* makeThing() {\n"
2535+
" Thing* n = new Thing();\n"
2536+
" return n;\n"
2537+
"}\n"
2538+
"Thing::Thing() {\n"
2539+
" g_things.push_back(this);\n"
2540+
"}\n"
2541+
"void f() {\n"
2542+
" makeThing();\n"
2543+
" for(Thing* t : g_things) {\n"
2544+
" delete t;\n"
2545+
" }\n"
2546+
"}\n");
2547+
ASSERT_EQUALS("", errout.str());
25242548
}
25252549
};
25262550
REGISTER_TEST(TestMemleakNoVar)

0 commit comments

Comments
 (0)