Skip to content

Commit 06cca97

Browse files
committed
Fix ExecuteAndWait with empty environment on Windows (llvm#158719)
CreateProcessW requires that the environemnt block to be always double null-terminated even with an empty environemnt. https://learn.microsoft.com/en-us/windows/win32/procthread/environment-variables The attached test fails this way without the fix. C:\Users\hiroshi\upstream\llvm-project\llvm\unittests\Support\ProgramTest.cpp(697): error: Value of: ExecutionFailed Actual: true Expected: false Couldn't execute program 'C:\Users\hiroshi\upstream\llvm-project\build\unittests\Support\SupportTests.exe': The parameter is incorrect. (0x57) (Cherry picked from commit 81c55e6)
1 parent ae63caf commit 06cca97

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

llvm/lib/Support/Windows/Program.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ static bool Execute(ProcessInfo &PI, StringRef Program,
220220
llvm::append_range(EnvBlock, EnvString);
221221
EnvBlock.push_back(0);
222222
}
223+
// If an empty environment (*Env is size zero), we need to
224+
// terminate with two nulls.
225+
if (Env->size() == 0)
226+
EnvBlock.push_back(0);
223227
EnvBlock.push_back(0);
224228
}
225229

llvm/unittests/Support/ProgramTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,22 @@ TEST_F(ProgramEnvTest, TestExecuteWithNoStacktraceHandler) {
597597
ASSERT_EQ(0, RetCode);
598598
}
599599

600+
TEST_F(ProgramEnvTest, TestExecuteEmptyEnvironment) {
601+
using namespace llvm::sys;
602+
603+
std::string Executable =
604+
sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
605+
StringRef argv[] = {
606+
Executable,
607+
"--gtest_filter=" // A null invocation to avoid infinite recursion
608+
};
609+
610+
std::string Error;
611+
bool ExecutionFailed;
612+
int RetCode = ExecuteAndWait(Executable, argv, ArrayRef<StringRef>{}, {}, 0,
613+
0, &Error, &ExecutionFailed);
614+
EXPECT_FALSE(ExecutionFailed) << Error;
615+
ASSERT_EQ(0, RetCode);
616+
}
617+
600618
} // end anonymous namespace

0 commit comments

Comments
 (0)