Skip to content

Commit f97b4be

Browse files
committed
move EvalState into worker to avoid stack-use-after-return
1 parent eb8a3fe commit f97b4be

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

src/nix-eval-jobs.cc

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ namespace {
6060
MyArgs myArgs; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
6161
}
6262

63-
using Processor = std::function<void(
64-
nix::ref<nix::EvalState> state, nix::Bindings &autoArgs,
65-
nix::AutoCloseFD &to, nix::AutoCloseFD &from, MyArgs &args)>;
63+
using Processor = std::function<void(MyArgs &myArgs, nix::AutoCloseFD &to,
64+
nix::AutoCloseFD &from)>;
6665

6766
/* Auto-cleanup of fork's process and fds. */
6867
struct Proc {
@@ -89,15 +88,7 @@ struct Proc {
8988
nix::lvlDebug,
9089
nix::fmt("created worker process %d", getpid()));
9190
try {
92-
auto evalStore = myArgs.evalStoreUrl
93-
? nix::openStore(*myArgs.evalStoreUrl)
94-
: nix::openStore();
95-
auto state = std::make_shared<nix::EvalState>(
96-
myArgs.lookupPath, evalStore, nix::fetchSettings,
97-
nix::evalSettings);
98-
nix::Bindings &autoArgs = *myArgs.getAutoArgs(*state);
99-
proc(nix::ref<nix::EvalState>(state), autoArgs, *to, *from,
100-
myArgs);
91+
proc(myArgs, *to, *from);
10192
} catch (nix::Error &e) {
10293
nlohmann::json err;
10394
const auto &msg = e.msg();

src/worker.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,16 @@ auto attrPathJoin(nlohmann::json input) -> std::string {
8484
} // namespace
8585

8686
void worker(
87-
nix::ref<nix::EvalState> state, nix::Bindings &autoArgs,
87+
MyArgs &args,
8888
nix::AutoCloseFD &to, // NOLINT(bugprone-easily-swappable-parameters)
89-
nix::AutoCloseFD &from, MyArgs &args) {
89+
nix::AutoCloseFD &from) {
90+
91+
auto evalStore = args.evalStoreUrl ? nix::openStore(*args.evalStoreUrl)
92+
: nix::openStore();
93+
auto state = nix::make_ref<nix::EvalState>(
94+
args.lookupPath, evalStore, nix::fetchSettings, nix::evalSettings);
95+
nix::Bindings &autoArgs = *args.getAutoArgs(*state);
96+
9097
nix::Value *vRoot = [&]() {
9198
if (args.flake) {
9299
auto [flakeRef, fragment, outputSpec] =

src/worker.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ class EvalState;
1212
template <typename T> class ref;
1313
} // namespace nix
1414

15-
void worker(nix::ref<nix::EvalState> state, nix::Bindings &autoArgs,
16-
nix::AutoCloseFD &to, nix::AutoCloseFD &from, MyArgs &args);
15+
void worker(MyArgs &args, nix::AutoCloseFD &to, nix::AutoCloseFD &from);

0 commit comments

Comments
 (0)