From 2f5791e00c029319ab2da70c8bd0aac090f8bb93 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 11 Sep 2023 00:38:34 +0300 Subject: [PATCH] Use a dynamic buffer when retrieving environment variables on Windows --- source/core/windows/environ.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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) -- 2.45.2