Project

General

Profile

Actions

Bug #12355

closed

[PATCH] exec, system, and spawn fail when Array#to_hash is defined and their first argument is an Array

Added by fimmtiu (Dennis Taylor) about 8 years ago. Updated almost 8 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
[ruby-core:75388]

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

process-to-hash.patch (414 Bytes) process-to-hash.patch One-line patch for an argument-parsing bug in process.c. fimmtiu (Dennis Taylor), 05/06/2016 06:29 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0