diff --git a/llvm/lib/Support/Windows/Program.inc b/llvm/lib/Support/Windows/Program.inc index 799af5559966c..d8d2e181aa337 100644 --- a/llvm/lib/Support/Windows/Program.inc +++ b/llvm/lib/Support/Windows/Program.inc @@ -220,6 +220,10 @@ static bool Execute(ProcessInfo &PI, StringRef Program, llvm::append_range(EnvBlock, EnvString); EnvBlock.push_back(0); } + // If an empty environment (*Env is size zero), we need to + // terminate with two nulls. + if (Env->size() == 0) + EnvBlock.push_back(0); EnvBlock.push_back(0); } diff --git a/llvm/unittests/Support/ProgramTest.cpp b/llvm/unittests/Support/ProgramTest.cpp index d30bf458f233c..eac0246d8c59e 100644 --- a/llvm/unittests/Support/ProgramTest.cpp +++ b/llvm/unittests/Support/ProgramTest.cpp @@ -680,4 +680,22 @@ TEST_F(ProgramEnvTest, TestExecuteWithNoStacktraceHandler) { ASSERT_EQ(0, RetCode); } +TEST_F(ProgramEnvTest, TestExecuteEmptyEnvironment) { + using namespace llvm::sys; + + std::string Executable = + sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1); + StringRef argv[] = { + Executable, + "--gtest_filter=" // A null invocation to avoid infinite recursion + }; + + std::string Error; + bool ExecutionFailed; + int RetCode = ExecuteAndWait(Executable, argv, ArrayRef{}, {}, 0, + 0, &Error, &ExecutionFailed); + EXPECT_FALSE(ExecutionFailed) << Error; + ASSERT_EQ(0, RetCode); +} + } // end anonymous namespace