[Lldb-commits] [lldb] [lldb][windows] Use GetEnvironmentVariableW instead of _wgetenv (PR #199015)
via lldb-commits
lldb-commits at lists.llvm.org
Thu May 21 05:27:47 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Charles Zablit (charles-zablit)
<details>
<summary>Changes</summary>
`_wgetenv` is not thread-safe per Microsoft docs:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-wgetenv?view=msvc-170.
Use `GetEnvironmentVariableW` with a sized buffer instead.
---
Full diff: https://github.com/llvm/llvm-project/pull/199015.diff
1 Files Affected:
- (modified) lldb/source/Host/windows/HostInfoWindows.cpp (+9-3)
``````````diff
diff --git a/lldb/source/Host/windows/HostInfoWindows.cpp b/lldb/source/Host/windows/HostInfoWindows.cpp
index 0b0cda49a64c0..8f4cb63a4d18a 100644
--- a/lldb/source/Host/windows/HostInfoWindows.cpp
+++ b/lldb/source/Host/windows/HostInfoWindows.cpp
@@ -127,9 +127,15 @@ bool HostInfoWindows::GetEnvironmentVar(const std::string &var_name,
if (!llvm::ConvertUTF8toWide(var_name, wvar_name))
return false;
- if (const wchar_t *wvar = _wgetenv(wvar_name.c_str()))
- return llvm::convertWideToUTF8(wvar, var);
- return false;
+ DWORD len = ::GetEnvironmentVariableW(wvar_name.c_str(), nullptr, 0);
+ if (len == 0)
+ return false;
+ std::vector<wchar_t> buffer(len);
+ DWORD written =
+ ::GetEnvironmentVariableW(wvar_name.c_str(), buffer.data(), len);
+ if (written == 0 || written >= len)
+ return false;
+ return llvm::convertWideToUTF8(buffer.data(), var);
}
static llvm::ManagedStatic<WindowsUserIDResolver> g_user_id_resolver;
``````````
</details>
https://github.com/llvm/llvm-project/pull/199015
More information about the lldb-commits
mailing list