diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs index 05e093434be1b..bc71c150550a4 100644 --- a/library/std/src/process/tests.rs +++ b/library/std/src/process/tests.rs @@ -399,3 +399,12 @@ fn test_command_implements_send_sync() { fn take_send_sync_type(_: T) {} take_send_sync_type(Command::new("")) } + +// Ensure that starting a process with no environment variables works on Windows. +// This will fail if the environment block is ill-formed. +#[test] +#[cfg(windows)] +fn env_empty() { + let p = Command::new("cmd").args(&["/C", "exit 0"]).env_clear().spawn(); + assert!(p.is_ok()); +} diff --git a/library/std/src/sys/windows/process.rs b/library/std/src/sys/windows/process.rs index 81dbea4a06739..14de2530842f8 100644 --- a/library/std/src/sys/windows/process.rs +++ b/library/std/src/sys/windows/process.rs @@ -530,6 +530,12 @@ fn make_envp(maybe_env: Option>) -> io::Result<(*mut if let Some(env) = maybe_env { let mut blk = Vec::new(); + // If there are no environment variables to set then signal this by + // pushing a null. + if env.is_empty() { + blk.push(0); + } + for (k, v) in env { blk.extend(ensure_no_nuls(k.0)?.encode_wide()); blk.push('=' as u16);