From ac6a5aab45ef721e92a28e1b0515aa2a514d740b Mon Sep 17 00:00:00 2001 From: Ben Dunbobbin Date: Fri, 18 Aug 2017 16:55:44 +0000 Subject: [PATCH] [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 --- llvm/lib/Support/Windows/Process.inc | 5 +++-- llvm/unittests/Support/ProcessTest.cpp | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Support/Windows/Process.inc b/llvm/lib/Support/Windows/Process.inc index 18aef610d54a..3fe9f89f1ef5 100644 --- a/llvm/lib/Support/Windows/Process.inc +++ b/llvm/lib/Support/Windows/Process.inc @@ -129,9 +129,10 @@ Optional 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. diff --git a/llvm/unittests/Support/ProcessTest.cpp b/llvm/unittests/Support/ProcessTest.cpp index 298a0a373234..37587bf47996 100644 --- a/llvm/unittests/Support/ProcessTest.cpp +++ b/llvm/unittests/Support/ProcessTest.cpp @@ -42,10 +42,18 @@ TEST(ProcessTest, None) { 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__"));