Skip to content

Commit 55e2dc6

Browse files
authored
fix #12238: FP memleak when passing buffer in vector (#7696)
1 parent be84139 commit 55e2dc6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/checkleakautovar.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,16 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
396396
}
397397
}
398398

399+
if (tok->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT) {
400+
const Token *newTok = tok->astOperand1();
401+
const Token *oldTok = tok->astOperand2();
402+
if (newTok && newTok->varId() && oldTok && oldTok->varId()) {
403+
leakIfAllocated(newTok, varInfo);
404+
// no multivariable checking currently => bail out for rhs variables
405+
varInfo.erase(oldTok->varId());
406+
}
407+
}
408+
399409
auto isAssignment = [](const Token* varTok) -> const Token* {
400410
if (varTok->varId()) {
401411
const Token* top = varTok;

test/testleakautovar.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class TestLeakAutoVar : public TestFixture {
180180
TEST_CASE(return9);
181181
TEST_CASE(return10);
182182
TEST_CASE(return11); // #13098
183+
TEST_CASE(return12); // #12238
183184

184185
// General tests: variable type, allocation type, etc
185186
TEST_CASE(test1);
@@ -2832,6 +2833,17 @@ class TestLeakAutoVar : public TestFixture {
28322833
ASSERT_EQUALS("[test.c:7:5]: (error) Memory leak: ptr [memleak]\n", errout_str());
28332834
}
28342835

2836+
void return12() { // #12238
2837+
CheckOptions options;
2838+
options.cpp = true;
2839+
check("void f(size_t size) {\n"
2840+
" void* buffer = malloc(size);\n"
2841+
" std::vector<void*> v{ buffer };\n"
2842+
" x->g(v);\n"
2843+
"}\n", options);
2844+
ASSERT_EQUALS("", errout_str());
2845+
}
2846+
28352847
void test1() {
28362848
check("void f(double*&p) {\n" // 3809
28372849
" p = malloc(0x100);\n"

0 commit comments

Comments
 (0)