Skip to content

Commit 93b774a

Browse files
committed
Don't set STARTF_USESTDHANDLES if none are set
1 parent c43210f commit 93b774a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

library/std/src/sys/windows/process.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,6 @@ impl Command {
252252
) -> io::Result<(Process, StdioPipes)> {
253253
let maybe_env = self.env.capture_if_changed();
254254

255-
let mut si = zeroed_startupinfo();
256-
si.cb = mem::size_of::<c::STARTUPINFO>() as c::DWORD;
257-
si.dwFlags = c::STARTF_USESTDHANDLES;
258-
259255
let child_paths = if let Some(env) = maybe_env.as_ref() {
260256
env.get(&EnvKey::new("PATH")).map(|s| s.as_os_str())
261257
} else {
@@ -314,9 +310,21 @@ impl Command {
314310
let stdin = stdin.to_handle(c::STD_INPUT_HANDLE, &mut pipes.stdin)?;
315311
let stdout = stdout.to_handle(c::STD_OUTPUT_HANDLE, &mut pipes.stdout)?;
316312
let stderr = stderr.to_handle(c::STD_ERROR_HANDLE, &mut pipes.stderr)?;
317-
si.hStdInput = stdin.as_raw_handle();
318-
si.hStdOutput = stdout.as_raw_handle();
319-
si.hStdError = stderr.as_raw_handle();
313+
314+
let mut si = zeroed_startupinfo();
315+
si.cb = mem::size_of::<c::STARTUPINFO>() as c::DWORD;
316+
317+
// If at least one of stdin, stdout or stderr are set (i.e. are non null)
318+
// then set the `hStd` fields in `STARTUPINFO`.
319+
// Otherwise skip this and allow the OS to apply its default behaviour.
320+
// This provides more consistent behaviour between Win7 and Win8+.
321+
let is_set = |stdio: &Handle| !stdio.as_raw_handle().is_null();
322+
if is_set(&stderr) || is_set(&stdout) || is_set(&stdin) {
323+
si.dwFlags |= c::STARTF_USESTDHANDLES;
324+
si.hStdInput = stdin.as_raw_handle();
325+
si.hStdOutput = stdout.as_raw_handle();
326+
si.hStdError = stderr.as_raw_handle();
327+
}
320328

321329
unsafe {
322330
cvt(c::CreateProcessW(

0 commit comments

Comments
 (0)