]> git.tdb.fi Git - libs/core.git/commitdiff
Use a dynamic buffer when retrieving environment variables on Windows
authorMikko Rasa <tdb@tdb.fi>
Sun, 10 Sep 2023 21:38:34 +0000 (00:38 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 10 Sep 2023 21:53:36 +0000 (00:53 +0300)
source/core/windows/environ.cpp

index 1bc61dee0f0bffb086eb0053f123d0815430bd43..2dd9d4cfec2816fd793ff37ffa7fc78f0e033e8a 100644 (file)
@@ -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)