From 365a3586a94dc9ca675ddb02a86e116e8161481a Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Sat, 19 Jun 2021 09:46:34 +0100 Subject: [PATCH 1/2] Windows: Fix `Command::env_clear` so it works Previously, it would error unless at least one new environment variable was added. --- library/std/src/sys/windows/process.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/std/src/sys/windows/process.rs b/library/std/src/sys/windows/process.rs index 81dbea4a067..14de2530842 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); From 16145a99528c862cfaca87e836fdd844ba155567 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Thu, 24 Jun 2021 09:32:24 +0100 Subject: [PATCH 2/2] Test that `env_clear` works on Windows --- library/std/src/process/tests.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs index 05e093434be..bc71c150550 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()); +}