Index: lib/Support/Windows/Process.inc =================================================================== --- lib/Support/Windows/Process.inc +++ lib/Support/Windows/Process.inc @@ -129,15 +129,19 @@ size_t Size = MAX_PATH; do { Buf.reserve(Size); + SetLastError(NO_ERROR); Size = - GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity()); - if (Size == 0) + GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity()); + if (Size == 0 && ERROR_ENVVAR_NOT_FOUND == GetLastError()) return None; // Try again with larger buffer. } while (Size > Buf.capacity()); Buf.set_size(Size); + if (Size == 0u) + return std::string(); + // Convert the result from UTF-16 to UTF-8. SmallVector Res; if (windows::UTF16ToUTF8(Buf.data(), Size, Res)) Index: unittests/Support/ProcessTest.cpp =================================================================== --- unittests/Support/ProcessTest.cpp +++ unittests/Support/ProcessTest.cpp @@ -42,10 +42,18 @@ Optional val( Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__")); EXPECT_FALSE(val.hasValue()); -} +} #endif #ifdef LLVM_ON_WIN32 + +TEST(ProcessTest, EmptyVal) { + SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", ""); + Optional val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__")); + EXPECT_TRUE(val.hasValue()); + EXPECT_STREQ("", val->c_str()); +} + TEST(ProcessTest, Wchar) { SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs"); Optional val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));