Skip to content

Commit 9716444

Browse files
committed
Fix ExecuteAndWait with empty environment on Windows
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)
1 parent ba576d3 commit 9716444

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
@@ -680,4 +680,22 @@ TEST_F(ProgramEnvTest, TestExecuteWithNoStacktraceHandler) {
680680
ASSERT_EQ(0, RetCode);
681681
}
682682

683+
TEST_F(ProgramEnvTest, TestExecuteEmptyEnvironment) {
684+
using namespace llvm::sys;
685+
686+
std::string Executable =
687+
sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
688+
StringRef argv[] = {
689+
Executable,
690+
"--gtest_filter=" // A null invocation to avoid infinite recursion
691+
};
692+
693+
std::string Error;
694+
bool ExecutionFailed;
695+
int RetCode = ExecuteAndWait(Executable, argv, ArrayRef<StringRef>{}, {}, 0,
696+
0, &Error, &ExecutionFailed);
697+
EXPECT_FALSE(ExecutionFailed) << Error;
698+
ASSERT_EQ(0, RetCode);
699+
}
700+
683701
} // end anonymous namespace

0 commit comments

Comments
 (0)