From 1b64aa68b9ff438f10d9880d40ea06751706be81 Mon Sep 17 00:00:00 2001
From: zhenhaonong <zhenhaonong@tencent.com>
Date: Thu, 22 Sep 2022 10:01:26 +0800
Subject: cmSystemTools: Fix encoding of whole-environment lookup on Windows

On Windows, `environ` is encoded by `CP_ACP`, which may be different
from `KWSYS_ENCODING_DEFAULT_CODEPAGE`.  When environment variables
include a unicode character, they may be corrupted.  Use `_wenviron`
instead.
---
 Source/cmSystemTools.cxx | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index 8e77afa..5737cc1 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -1642,9 +1642,18 @@ std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
 {
   std::vector<std::string> env;
   int cc;
+#  ifdef _WIN32
+  // if program starts with main, _wenviron is initially NULL, call to
+  // _wgetenv and create wide-character string environment
+  _wgetenv(L"");
+  for (cc = 0; _wenviron[cc]; ++cc) {
+    env.emplace_back(cmsys::Encoding::ToNarrow(_wenviron[cc]));
+  }
+#  else
   for (cc = 0; environ[cc]; ++cc) {
     env.emplace_back(environ[cc]);
   }
+#  endif
   return env;
 }
 
-- 
cgit v0.12