From: Mikko Rasa Date: Sun, 10 Sep 2023 21:38:34 +0000 (+0300) Subject: Use a dynamic buffer when retrieving environment variables on Windows X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2f5791e00c029319ab2da70c8bd0aac090f8bb93;p=libs%2Fcore.git Use a dynamic buffer when retrieving environment variables on Windows --- diff --git a/source/core/windows/environ.cpp b/source/core/windows/environ.cpp index 1bc61de..2dd9d4c 100644 --- a/source/core/windows/environ.cpp +++ b/source/core/windows/environ.cpp @@ -19,12 +19,19 @@ static Mutex &env_mutex() string getenv(const string &name) { MutexLock _lock(env_mutex()); - char buffer[1024]; - size_t result; - if(!getenv_s(&result, buffer, sizeof(buffer), name.c_str())) - return string(buffer, (result>0 ? result-1 : 0)); - else - return string(); + string value; + size_t length = 0; + if(!getenv_s(&length, nullptr, 0, name.c_str())) + { + value.resize(length); + if(!getenv_s(&length, &value[0], value.size(), name.c_str())) + { + if(!value.empty() && !value.back()) + value.pop_back(); + return value; + } + } + return string(); } void setenv(const string &name, const string &value)