From 98ef11677c43db9aa669768d9f0cbec704e8831c Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <edolstra@gmail.com>
Date: Tue, 10 Dec 2019 13:32:30 +0100
Subject: [PATCH] EvalState::callFunction(): Make FunctionCallTrace use less
 stack space

The FunctionCallTrace object consumes a few hundred bytes of stack
space, even when tracing is disabled. This was causing stack overflows:

  $ nix-instantiate '<nixpkgs> -A texlive.combined.scheme-full --dry-run
  error: stack overflow (possible infinite recursion)

This is with the default stack size of 8 MiB.

Putting the object on the heap reduces stack usage to < 5 MiB.
---
 src/libexpr/eval.cc | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index e2070d546..4be4cfeea 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -1091,10 +1091,9 @@ void EvalState::callPrimOp(Value & fun, Value & arg, Value & v, const Pos & pos)
 
 void EvalState::callFunction(Value & fun, Value & arg, Value & v, const Pos & pos)
 {
-    std::optional<FunctionCallTrace> trace;
-    if (evalSettings.traceFunctionCalls) {
-        trace.emplace(pos);
-    }
+    std::unique_ptr<FunctionCallTrace> trace;
+    if (evalSettings.traceFunctionCalls)
+        trace = std::make_unique<FunctionCallTrace>(pos);
 
     forceValue(fun, pos);
 
-- 
GitLab