Skip to content

Commit

Permalink
Avoid accessing uninitialized settings in own init (#11117)
Browse files Browse the repository at this point in the history
The default value for the setting was evaluated by
calling a method on the object _being currently constructed_,
so we were using it before all fields were initialized.

This has been fixed by making the called method static,
and not using the previously used fields at all.

But functionality hasn't changed!
The fields were usually always zero (by chance?) anyway,
meaning the conditional path was always taken.

Thus the current logic has been kept, the code simplified,
and UB removed.

This was found with the helper of UBSan.
  • Loading branch information
L-as authored Jul 17, 2024
1 parent b230c01 commit 464e592
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/libexpr/eval-settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ EvalSettings::EvalSettings(bool & readOnlyMode, EvalSettings::LookupPathHooks lo
builtinsAbortOnWarn = true;
}

Strings EvalSettings::getDefaultNixPath() const
Strings EvalSettings::getDefaultNixPath()
{
Strings res;
auto add = [&](const Path & p, const std::string & s = std::string()) {
Expand All @@ -69,11 +69,9 @@ Strings EvalSettings::getDefaultNixPath() const
}
};

if (!restrictEval && !pureEval) {
add(getNixDefExpr() + "/channels");
add(rootChannelsDir() + "/nixpkgs", "nixpkgs");
add(rootChannelsDir());
}
add(getNixDefExpr() + "/channels");
add(rootChannelsDir() + "/nixpkgs", "nixpkgs");
add(rootChannelsDir());

return res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/eval-settings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct EvalSettings : Config

bool & readOnlyMode;

Strings getDefaultNixPath() const;
static Strings getDefaultNixPath();

static bool isPseudoUrl(std::string_view s);

Expand Down

0 comments on commit 464e592

Please sign in to comment.