Fix crash in CheckLeakAutoVar (f'up to #12186) (#5683)

This commit is contained in:
chrchr-github 2023-11-20 18:26:05 +01:00 committed by GitHub
parent 036df0aca9
commit d7c7a39afe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -1109,8 +1109,8 @@ void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndO
}
// don't warn when returning after checking return value of outparam allocation
const Scope* scope = tok->scope();
if (scope->type == Scope::ScopeType::eIf || scope->type== Scope::ScopeType::eElse) {
if (it->second.allocTok && (tok->scope()->type == Scope::ScopeType::eIf || tok->scope()->type== Scope::ScopeType::eElse)) {
const Scope* scope = tok->scope();
if (scope->type == Scope::ScopeType::eElse) {
scope = scope->bodyStart->tokAt(-2)->scope();
}

View File

@ -1594,6 +1594,16 @@ private:
" s->p = NULL;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
const Settings s = settingsBuilder().library("std.cfg").build();
check("struct S {};\n"
"void f(int i, std::vector<std::unique_ptr<S>> &v) {\n"
" if (i < 1) {\n"
" auto s = new S;\n"
" v.push_back(std::unique_ptr<S>(s));\n"
" }\n"
"}\n", &s);
ASSERT_EQUALS("", errout.str()); // don't crash
}
void goto1() {