From d7c7a39afe5635fe80a1c4f0c43bb1f43de2c390 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 20 Nov 2023 18:26:05 +0100 Subject: [PATCH] Fix crash in CheckLeakAutoVar (f'up to #12186) (#5683) --- lib/checkleakautovar.cpp | 4 ++-- test/testleakautovar.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 2b3b65c8c..d99a28d03 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -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(); } diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 0b70e28f2..700292c9c 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -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> &v) {\n" + " if (i < 1) {\n" + " auto s = new S;\n" + " v.push_back(std::unique_ptr(s));\n" + " }\n" + "}\n", &s); + ASSERT_EQUALS("", errout.str()); // don't crash } void goto1() {