about summary refs log tree commit diff
path: root/library/std/src/process/tests.rs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-12-23 00:28:56 +0100
committerGitHub <noreply@github.com>2021-12-23 00:28:56 +0100
commit3afed8fc709cc174e9518269694b344b7487cb65 (patch)
tree8fcbec674ee1e35fec378987671a840a0f8c8757 /library/std/src/process/tests.rs
parent051d91a5ce6c6a7fa2f4759a4df546254fb5cccc (diff)
parentde764a7ccbbcaf90db88569ce7a8b5e2214cfcd8 (diff)
downloadrust-3afed8fc709cc174e9518269694b344b7487cb65.tar.gz
rust-3afed8fc709cc174e9518269694b344b7487cb65.zip
Rollup merge of #92208 - ChrisDenton:win-bat-cmd, r=dtolnay
Quote bat script command line

Fixes #91991

[`CreateProcessW`](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#parameters) should only be used to run exe files but it does have some (undocumented) special handling for files with `.bat` and `.cmd` extensions. Essentially those magic extensions will cause the parameters to be automatically rewritten. Example pseudo Rust code (note that `CreateProcess` starts with an optional application name followed by the application arguments):
```rust
// These arguments...
CreateProcess(None, `@"foo.bat` "hello world""`@,` ...);
// ...are rewritten as
CreateProcess(Some(r"C:\Windows\System32\cmd.exe"), `@""foo.bat` "hello world"""`@,` ...);
```

However, when setting the first parameter (the application name) as we now do, it will omit the extra level of quotes around the arguments:

```rust
// These arguments...
CreateProcess(Some("foo.bat"), `@"foo.bat` "hello world""`@,` ...);
// ...are rewritten as
CreateProcess(Some(r"C:\Windows\System32\cmd.exe"), `@"foo.bat` "hello world""`@,` ...);
```

This means the arguments won't be passed to the script as intended.

Note that running batch files this way is undocumented but people have relied on this so we probably shouldn't break it.
Diffstat (limited to 'library/std/src/process/tests.rs')
-rw-r--r--library/std/src/process/tests.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs
index 61ab0eb55e6..e5cdc473706 100644
--- a/library/std/src/process/tests.rs
+++ b/library/std/src/process/tests.rs
@@ -420,3 +420,22 @@ fn env_empty() {
     let p = Command::new("cmd").args(&["/C", "exit 0"]).env_clear().spawn();
     assert!(p.is_ok());
 }
+
+// See issue #91991
+#[test]
+#[cfg(windows)]
+fn run_bat_script() {
+    let tempdir = crate::sys_common::io::test::tmpdir();
+    let script_path = tempdir.join("hello.cmd");
+
+    crate::fs::write(&script_path, "@echo Hello, %~1!").unwrap();
+    let output = Command::new(&script_path)
+        .arg("fellow Rustaceans")
+        .stdout(crate::process::Stdio::piped())
+        .spawn()
+        .unwrap()
+        .wait_with_output()
+        .unwrap();
+    assert!(output.status.success());
+    assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "Hello, fellow Rustaceans!");
+}