Bug #12355
closed[PATCH] exec, system, and spawn fail when Array#to_hash is defined and their first argument is an Array
Description
When any gem which adds a to_hash method to Array is loaded (extlib, dropbox, others), Kernel.exec, Kernel.system, and Kernel.spawn will fail when you specify the program name with an array. Behold:
irb(main):001:0> ex = "/usr/local/bin/cowsay"; Kernel.exec([ex, ex], "-t", "Hello world")
Errno::ENOENT: No such file or directory - -t
from (irb):1:in `exec'
You can duplicate it yourself in irb without loading any gems by just defining an Array#to_hash method of your own:
irb(main):001:0> class Array; def to_hash; raise "HONK"; end; end; ex = "/usr/local/bin/cowsay"; Kernel.exec([ex, ex], "-t", "Hello world")
RuntimeError: HONK
from (irb):1:in `to_hash'
from (irb):1:in `exec'
(Let's leave aside the issue of whether it's a good idea or not to add a to_hash method to Array in the first place; the basic language functionality should still be robust in the presence of people doing silly things.)
The problem is that, because it thinks that the first parameter is something hash-like, it assumes that it's the "env" parameter, and goes on to use the first argument to the program ("-t") as the program name. I've attached a one-line patch to process.c to fix the argument parsing; it'll ensure that the first argument is not an array before trying to convert it to a hash.
Output with the patch:
irb(main):001:0> class Array; def to_hash; raise "HONK"; end; end; ex = "/usr/local/bin/cowsay"; Kernel.exec([ex, ex], "-t", "Hello world")
_____________
< Hello world >
-------------
\ ^__^
\ (--)\_______
(__)\ )\/\
||----w |
|| ||
Files
Updated by fimmtiu (Dennis Taylor) about 8 years ago
- Subject changed from exec, system, and spawn fail when Array#to_hash is defined and its first argument is an Array to exec, system, and spawn fail when Array#to_hash is defined and their first argument is an Array
Updated by fimmtiu (Dennis Taylor) about 8 years ago
- ruby -v changed from 2.3.0 to ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
Updated by fimmtiu (Dennis Taylor) about 8 years ago
- Subject changed from exec, system, and spawn fail when Array#to_hash is defined and their first argument is an Array to [PATCH] exec, system, and spawn fail when Array#to_hash is defined and their first argument is an Array
Updated by nobu (Nobuyoshi Nakada) about 8 years ago
- Status changed from Open to Closed
Applied in changeset r54934.
process.c: argument types over conversion
- process.c (rb_exec_getargs): honor the expected argument types
over the conversion method. the basic language functionality
should be robust. [ruby-core:75388] [Bug #12355]
Updated by usa (Usaku NAKAMURA) about 8 years ago
- Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: WONTFIX, 2.2: REQUIRED, 2.3: REQUIRED
Updated by usa (Usaku NAKAMURA) about 8 years ago
- Backport changed from 2.1: WONTFIX, 2.2: REQUIRED, 2.3: REQUIRED to 2.1: WONTFIX, 2.2: DONE, 2.3: REQUIRED
ruby_2_2 r55362 merged revision(s) 54934.
Updated by nagachika (Tomoyuki Chikanaga) about 8 years ago
- Backport changed from 2.1: WONTFIX, 2.2: DONE, 2.3: REQUIRED to 2.1: WONTFIX, 2.2: DONE, 2.3: DONE
ruby_2_3 r55401 merged revision(s) 54934.