From 31785b48fdbbb615d4c90f0c09388a0f927f23e3 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 7 Sep 2022 09:41:16 +0200 Subject: [PATCH] Expression: Decrease `frame.Depth` only when calling `IncreaseStackDepth()` succeeds This ensures that `frame.Depth` is only decreased when preceding `frame.IncreaseStackDepth()` callee was successful. This way, `frame.Depth` will have the same depth prior to and after evaluating a frame. --- lib/config/expression.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index 6a709ac6a..93ce79a80 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -13,6 +13,7 @@ #include "base/loader.hpp" #include "base/reference.hpp" #include "base/namespace.hpp" +#include "base/defer.hpp" #include #include @@ -46,22 +47,20 @@ ExpressionResult Expression::Evaluate(ScriptFrame& frame, DebugHint *dhint) cons #endif /* I2_DEBUG */ frame.IncreaseStackDepth(); + + Defer decreaseStackDepth([&frame]{ + frame.DecreaseStackDepth(); + }); + ExpressionResult result = DoEvaluate(frame, dhint); - frame.DecreaseStackDepth(); return result; } catch (ScriptError& ex) { - frame.DecreaseStackDepth(); - ScriptBreakpoint(frame, &ex, GetDebugInfo()); throw; } catch (const std::exception& ex) { - frame.DecreaseStackDepth(); - BOOST_THROW_EXCEPTION(ScriptError("Error while evaluating expression: " + String(ex.what()), GetDebugInfo()) << boost::errinfo_nested_exception(boost::current_exception())); } - - frame.DecreaseStackDepth(); } bool Expression::GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const