From a58e38dab7f6a9b9d217f6163fe8b565f19a6405 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 19 Nov 2024 17:30:58 +0100 Subject: [PATCH] Make EvalState::getBuiltin safe for missing attr --- src/libexpr/eval.cc | 6 +++++- src/libexpr/eval.hh | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 0283a14d6..dd14f485e 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -525,7 +525,11 @@ Value * EvalState::addPrimOp(PrimOp && primOp) Value & EvalState::getBuiltin(const std::string & name) { - return *baseEnv.values[0]->attrs()->find(symbols.create(name))->value; + auto it = baseEnv.values[0]->attrs()->get(symbols.create(name)); + if (it) + return *it->value; + else + throw EvalError("builtin '%1%' not found", name); } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 742f1cafe..01c725930 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -623,6 +623,11 @@ private: public: + /** + * Retrieve a specific builtin, equivalent to evaluating `builtins.${name}`. + * @param name The attribute name of the builtin to retrieve. + * @throws EvalError if the builtin does not exist. + */ Value & getBuiltin(const std::string & name); struct Doc