[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;
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 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
return None;
// Try again with larger buffer.

View File

@ -42,10 +42,18 @@ TEST(ProcessTest, None) {
Optional<std::string> 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<std::string> 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<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));