Skip to content

Commit

Permalink
Forbid nested debuggers
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Feb 3, 2024
1 parent 081dc5d commit 29b0d78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ std::unique_ptr<ValMap> mapStaticEnvBindings(const SymbolTable & st, const Stati

void EvalState::runDebugRepl(const Error * error, const Env & env, const Expr & expr)
{
// double check we've got the debugRepl function pointer.
if (!debugRepl)
// Make sure we have a debugger to run and we're not already in a debugger.
if (!debugRepl || inDebugger)
return;

auto dts =
Expand Down Expand Up @@ -832,6 +832,7 @@ void EvalState::runDebugRepl(const Error * error, const Env & env, const Expr &
auto se = getStaticEnv(expr);
if (se) {
auto vm = mapStaticEnvBindings(symbols, *se.get(), env);
DebuggerGuard _guard(inDebugger);
(debugRepl)(ref<EvalState>(shared_from_this()), *vm);
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ class ErrorBuilder
void debugThrow();
};

namespace {
/**
* Sets `inDebugger` to true on construction and false on destruction.
*/
class DebuggerGuard {
bool & inDebugger;
public:
DebuggerGuard(bool & inDebugger) : inDebugger(inDebugger) {
inDebugger = true;
}
~DebuggerGuard() {
inDebugger = false;
}
};
}


class EvalState : public std::enable_shared_from_this<EvalState>
{
Expand Down Expand Up @@ -260,6 +276,7 @@ public:
void (* debugRepl)(ref<EvalState> es, const ValMap & extraEnv);
bool debugStop;
bool debugQuit;
bool inDebugger = false;
int trylevel;
std::list<DebugTrace> debugTraces;
std::map<const Expr*, const std::shared_ptr<const StaticEnv>> exprEnvs;
Expand Down

0 comments on commit 29b0d78

Please sign in to comment.