Skip to content

Commit ac6a5aa

Browse files
committed
[Support] env vars with empty values on windows
An environment variable can be in one of three states: 1. undefined. 2. defined with a non-empty value. 3. defined but with an empty value. The windows implementation did not support case 3 (it was not handling errors). The Linux implementation is already correct. Differential Revision: https://reviews.llvm.org/D36394 llvm-svn: 311174
1 parent 849f20e commit ac6a5aa

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

llvm/lib/Support/Windows/Process.inc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ Optional<std::string> Process::GetEnv(StringRef Name) {
129129
size_t Size = MAX_PATH;
130130
do {
131131
Buf.reserve(Size);
132+
SetLastError(NO_ERROR);
132133
Size =
133-
GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity());
134-
if (Size == 0)
134+
GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity());
135+
if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
135136
return None;
136137

137138
// Try again with larger buffer.

llvm/unittests/Support/ProcessTest.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ TEST(ProcessTest, None) {
4242
Optional<std::string> val(
4343
Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
4444
EXPECT_FALSE(val.hasValue());
45-
}
45+
}
4646
#endif
4747

4848
#ifdef LLVM_ON_WIN32
49+
50+
TEST(ProcessTest, EmptyVal) {
51+
SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", "");
52+
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
53+
EXPECT_TRUE(val.hasValue());
54+
EXPECT_STREQ("", val->c_str());
55+
}
56+
4957
TEST(ProcessTest, Wchar) {
5058
SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
5159
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));

0 commit comments

Comments
 (0)