Bug #10128

Quoting problem for arguments of Kernel.system, Kernel.exec on Windows

Added by Maxime Lapointe about 2 years ago.

ruby -v:
ruby 2.0.0p481 (2014-05-08) [x64-mingw32]


On Windows, the methods that call shell commands and receive the parameters individually sometimes do not wrap the parameters sent in quotes.
This results in Windows either splitting the parameter in 2 parameters or, worse, splitting the command in 2 commands.

I joined the file puts_first.bat, which simply outputs the first argument it received. When the parameter received was wrapped by ruby, you will see quotes, it's normal. Just run a irb from from the directory containing that file.

Lines that don't work properly (Using Kernel.exec will do the same thing):

# these write *hello*, then says 'world' is not recognized as an internal or external command
Kernel.system 'puts_first.bat', 'hello&world'
Kernel.system 'puts_first.bat', 'hello|world'

# these write *hello*
Kernel.system 'puts_first.bat', 'hello,world'
Kernel.system 'puts_first.bat', 'hello;world'
Kernel.system 'puts_first.bat', 'hello<world'

# this writes *hello* in the file world
Kernel.system 'puts_first.bat', 'hello>world'

# this writes *helloworld* without the ^
Kernel.system 'puts_first.bat', 'hello^world'

If we add a space anywhere in the above hello world strings, it will work as expected because ruby wraps the parameter if it finds a space.

# Ruby does try to wrap if it finds a double quote, but it escapes double quotes incorrectly:
# this writes *"hello\"world"*, double quotes should be escaped by putting 2 of them, so we should see: *"hello""world"*
Kernel.system 'puts_first.bat', 'hello"world'

# adding a space show the problem in action, this writes *"hello\"*
Kernel.system 'puts_first.bat', 'hello" world'

As a side note, the single quote is not special in Windows, so there is no need to wrap this (but I don't think it's a problem):

# this writes *"hello'world"*
Kernel.system 'puts_first.bat', "hello'world"

This bug also happens in 1.9.3, do you think this be backported?

Unless I did a mistake, this should be all of the problematic characters, I tested with every printable ascii characters.

Thank you

puts_first.bat (18 Bytes) Maxime Lapointe, 08/12/2014 04:01 PM

Also available in: Atom PDF