From cad6971c1d3b7409c4f9b78cfd8b0d4e659aa6f6 Mon Sep 17 00:00:00 2001 From: Dusk Banks Date: Fri, 31 Jan 2025 01:43:48 -0800 Subject: [PATCH] libexpr: minimize scope of `tryEval` `try`-`catch` only `AssertionError`s from the forcing of the `tryEval`'s argument should be caught. reducing the scope makes that explicit. Change-Id: I6de6e14dda07619134babe5ee24bb1afbfc5fec1 --- lix/libexpr/primops.cc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index bb2be33d5..178333277 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -643,7 +643,7 @@ static void prim_tryEval(EvalState & state, const PosIdx pos, Value * * args, Va { auto attrs = state.ctx.buildBindings(2); - { + const bool success = [&] { std::optional> trylevel; DebugState * savedDebug = nullptr; KJ_DEFER({ @@ -662,13 +662,16 @@ static void prim_tryEval(EvalState & state, const PosIdx pos, Value * * args, Va try { state.forceValue(*args[0], pos); - attrs.insert(state.ctx.s.value, args[0]); - attrs.alloc("success").mkBool(true); } catch (AssertionError & e) { - attrs.alloc(state.ctx.s.value).mkBool(false); - attrs.alloc("success").mkBool(false); + return false; } - } + return true; + }(); + if (success) + attrs.insert(state.ctx.s.value, args[0]); + else + attrs.alloc(state.ctx.s.value).mkBool(false); + attrs.alloc("success").mkBool(success); v.mkAttrs(attrs); }