[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
This commit is contained in:
Ben Dunbobbin 2017-08-18 16:55:44 +00:00
parent 849f20e4a2
commit ac6a5aab45
2 changed files with 12 additions and 3 deletions

View File

@ -129,9 +129,10 @@ Optional<std::string> Process::GetEnv(StringRef Name) {
size_t Size = MAX_PATH; size_t Size = MAX_PATH;
do { do {
Buf.reserve(Size); Buf.reserve(Size);
SetLastError(NO_ERROR);
Size = Size =
GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity()); GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity());
if (Size == 0) if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
return None; return None;
// Try again with larger buffer. // Try again with larger buffer.

View File

@ -42,10 +42,18 @@ TEST(ProcessTest, None) {
Optional<std::string> val( Optional<std::string> val(
Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__")); Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
EXPECT_FALSE(val.hasValue()); EXPECT_FALSE(val.hasValue());
} }
#endif #endif
#ifdef LLVM_ON_WIN32 #ifdef LLVM_ON_WIN32
TEST(ProcessTest, EmptyVal) {
SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", "");
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
EXPECT_TRUE(val.hasValue());
EXPECT_STREQ("", val->c_str());
}
TEST(ProcessTest, Wchar) { TEST(ProcessTest, Wchar) {
SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs"); SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__")); Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));