Bug #22199
closedwin32: Kernel#system: `AA BB\` arrives as `AA BB"`
Description
PS C:\> ruby -ve "system('python3','-c','import sys; print(sys.argv[1])', 'AA BB\\')"
ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [x64-mingw-ucrt]
AA BB"
AA BB\ was passed to Kernel#system, but python3.exe read it as AA BB".
What happens:
Kernel#systemcreates and passes the command line stringpython3 ... "AA BB\"toCreateProcessW, and- python3.exe (and anything else using the VC Runtime's command line parser) reads
"AA BB\"as an (accidentally unterminated)AA BB".
It reproduces when an argument contains a space and ends with \.
When an argument contains a space, Kernel#system wraps it in double quotes.
If the argument also ends with \, that backslash is taken to escape the closing quote.
Kernel#system should escape the final backslash(es) when it wraps an argument that ends with one.
In the example above it should pass python3 ... "AA BB\\" to CreateProcessW.
Here is a PR for that: https://github.com/ruby/ruby/pull/17941
Note that cmd.exe and batch files do not interpret backslash escapes, so the backslash must NOT be escaped when calling those.
The PR escapes only when (1) the call does not go through cmd.exe, (2) the argument is wrapped in double quotes, and (3) the argument ends with \.