libstore: Make sure that initNix has been called

Prevent bugs like https://github.com/cachix/cachix/pull/477
This commit is contained in:
Robert Hensing 2022-12-19 14:06:07 +01:00
parent 26c7602c39
commit aba6eb348e
5 changed files with 25 additions and 0 deletions

View file

@ -12,6 +12,7 @@ namespace nix {
class LibExprTest : public ::testing::Test {
public:
static void SetUpTestSuite() {
initLibStore();
initGC();
}

View file

@ -235,6 +235,7 @@ void initNix()
#endif
preloadNSS();
initLibStore();
}

View file

@ -291,4 +291,18 @@ void initPlugins()
settings.pluginFiles.pluginsLoaded = true;
}
static bool initLibStoreDone = false;
void assertLibStoreInitialized() {
if (!initLibStoreDone) {
printError("The program must call nix::initNix() before calling any libstore library functions.");
abort();
};
}
void initLibStore() {
initLibStoreDone = true;
}
}

View file

@ -984,4 +984,12 @@ std::vector<Path> getUserConfigFiles();
extern const std::string nixVersion;
/* NB: This is not sufficient. You need to call initNix() */
void initLibStore();
/* It's important to initialize before doing _anything_, which is why we
call upon the programmer to handle this correctly. However, we only add
this in a key locations, so as not to litter the code. */
void assertLibStoreInitialized();
}

View file

@ -458,6 +458,7 @@ Store::Store(const Params & params)
: StoreConfig(params)
, state({(size_t) pathInfoCacheSize})
{
assertLibStoreInitialized();
}