Skip to content

Commit d7c7a39

Browse files
Fix crash in CheckLeakAutoVar (f'up to #12186) (#5683)
1 parent 036df0a commit d7c7a39

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

lib/checkleakautovar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,8 @@ void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndO
11091109
}
11101110

11111111
// don't warn when returning after checking return value of outparam allocation
1112-
const Scope* scope = tok->scope();
1113-
if (scope->type == Scope::ScopeType::eIf || scope->type== Scope::ScopeType::eElse) {
1112+
if (it->second.allocTok && (tok->scope()->type == Scope::ScopeType::eIf || tok->scope()->type== Scope::ScopeType::eElse)) {
1113+
const Scope* scope = tok->scope();
11141114
if (scope->type == Scope::ScopeType::eElse) {
11151115
scope = scope->bodyStart->tokAt(-2)->scope();
11161116
}

test/testleakautovar.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,16 @@ class TestLeakAutoVar : public TestFixture {
15941594
" s->p = NULL;\n"
15951595
"}\n");
15961596
ASSERT_EQUALS("", errout.str());
1597+
1598+
const Settings s = settingsBuilder().library("std.cfg").build();
1599+
check("struct S {};\n"
1600+
"void f(int i, std::vector<std::unique_ptr<S>> &v) {\n"
1601+
" if (i < 1) {\n"
1602+
" auto s = new S;\n"
1603+
" v.push_back(std::unique_ptr<S>(s));\n"
1604+
" }\n"
1605+
"}\n", &s);
1606+
ASSERT_EQUALS("", errout.str()); // don't crash
15971607
}
15981608

15991609
void goto1() {

0 commit comments

Comments
 (0)