Avoid accessing uninitialized settings in own init (#11117)

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.
This commit is contained in:
Las Safin 2024-07-17 13:10:01 +01:00 committed by GitHub
parent b230c01f73
commit 464e5925cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 7 deletions

View file

@ -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()) {
@ -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;
}

View file

@ -43,7 +43,7 @@ struct EvalSettings : Config
bool & readOnlyMode;
Strings getDefaultNixPath() const;
static Strings getDefaultNixPath();
static bool isPseudoUrl(std::string_view s);