Skip to content

fix getenv usage

Spot contains many instances of

static const char* opt = getenv("SPOT_SOMETHING");

to ensure that opt is only read once (because getenv() is slow). However this pattern is dangerous when the library is used from python where it is tempting to alter the environment. In particular, if the environment is changed from Python after the above getenv has run, the opt pointer would be incorrect the next time it is used.

I guess we should instead use a pattern such as

static auto res = []() {
    const char* opt = getenv("SPOT_SOMETHING");
    return extract_the_info_we_need(opt);
}();

this way the computation is still done once, but we do not rely on a pointer that might change. So if someone changes the environment in Python after we have read it, we simply ignore further changes (and we do not crash).