Skip to content

Broken quote handling when spawning a process from a cmd script. #91991

Closed
@mwu-tow

Description

@mwu-tow

Code

I tried this code:

pub fn greet(script_path: &std::path::Path, name: &str) -> std::io::Result<std::process::ExitStatus> {
    std::process::Command::new(script_path)
        .arg(name)
        .spawn()?
        .wait()
}

pub fn main() -> std::io::Result<()> {
    let script_path = std::env::temp_dir().join("hello.cmd");
    std::fs::write(&script_path, "@echo Hello, %~1!")?;
    greet(&script_path, "world")?;
    greet(&script_path, "fellow Rustaceans")?;
    Ok(())
}

I expected to see this happen:

Hello, world!
Hello, fellow Rustaceans!

Instead, this happened:

Hello, world!
'C:\Users\mwurb\AppData\Local\Temp\hello.cmd" "fellow' is not recognized as an internal or external command,
operable program or batch file.

This is caused by different quote handling.
The first (correct) command line for the spawned process (as observed by procmon) is:

C:\WINDOWS\system32\cmd.exe /c ""C:\Users\mwurb\AppData\Local\Temp\hello.cmd" "fellow Rustaceans""

The broken one is:

C:\WINDOWS\system32\cmd.exe /c "C:\Users\mwurb\AppData\Local\Temp\hello.cmd" "fellow Rustaceans"

The command for cmd must be in additional set of quotes, as the first and last one are stripped.
It works when there are no spaces in the arguments due to the exception rule, see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd#remarks

Version it worked on

Stable. It worked up until nightly-2021-11-20

rustc 1.58.0-nightly (a77da2d45 2021-11-19)
binary: rustc
commit-hash: a77da2d454e6caa227a85b16410b95f93495e7e0
commit-date: 2021-11-19
host: x86_64-pc-windows-msvc
release: 1.58.0-nightly
LLVM version: 13.0.0     

Version with regression

nightly-2021-11-21

rustc 1.58.0-nightly (2885c4748 2021-11-20)
binary: rustc
commit-hash: 2885c474823637ae69c5967327327a337aebedb2
commit-date: 2021-11-20
host: x86_64-pc-windows-msvc
release: 1.58.0-nightly
LLVM version: 13.0.0

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-windowsOperating system: WindowsP-highHigh priorityT-libsRelevant to the library team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions