From b841b818d2adbbca85d0091864e053403be3afc1 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 1 Dec 2018 19:11:26 +0100 Subject: [PATCH] Fix 8872: Crash in LifetimeStore when there is no scope for variable This fixes crash in: ```cpp struct edit_line_paste_over { void operator()(agi::Context *c) override { paste_lines(c, true, [&](AssDialogue *new_line) -> AssDialogue * { AssDialogue *ret = paste_over(c->parent, pasteOverOptions, new_line, static_cast(&*pos)); return ret; }); } }; ``` --- lib/valueflow.cpp | 2 ++ test/testautovariables.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 8ccb845b1..29f8b8b35 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2751,6 +2751,8 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase*, ErrorLogger auto isCapturingVariable = [&](const Variable *var) { const Scope *scope = var->scope(); + if (!scope) + return false; if (scopes.count(scope) > 0) return false; if (scope->isNestedIn(bodyScope)) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index b69a6d75d..89c7f55a3 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -1510,6 +1510,7 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + <<<<<<< HEAD <<<<<<< HEAD check("void f() {\n" " struct b {\n" @@ -1540,6 +1541,38 @@ private: " A &&a = T{1, 2, 3}[1]();\n" "}\n"); ASSERT_EQUALS("", errout.str()); + ======= + // Make sure we dont hang + check("struct A;\n" + "void f() {\n" + " using T = A[3];\n" + " A &&a = T{1, 2, 3}[1];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + // Make sure we dont hang + check("struct A;\n" + "void f() {\n" + " using T = A[3];\n" + " A &&a = T{1, 2, 3}[1]();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + // Crash #8872 + check("struct a {\n" + " void operator()(b c) override {\n" + " d(c, [&] { c->e });\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + + check("struct a {\n" + " void operator()(b c) override {\n" + " d(c, [=] { c->e });\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + >>>>>>> 6586f2d354d3f9dc26d830b6e641f158a5c58432 } void danglingLifetimeFunction() {