From acaec7aa90bc428d94e42352a19a93303e5b73fa Mon Sep 17 00:00:00 2001 From: Alex Tambellini Date: Sat, 4 May 2013 01:00:14 -0400 Subject: [PATCH] Kernel.exec should raise an ArgumentError if the command includes a null byte --- spec/tags/1.8/ruby/core/kernel/exec_tags.txt | 2 -- spec/tags/1.9/ruby/core/kernel/exec_tags.txt | 2 -- src/org/jruby/RubyKernel.java | 13 +++++++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/spec/tags/1.8/ruby/core/kernel/exec_tags.txt b/spec/tags/1.8/ruby/core/kernel/exec_tags.txt index ac801550312..c99efa359fb 100644 --- a/spec/tags/1.8/ruby/core/kernel/exec_tags.txt +++ b/spec/tags/1.8/ruby/core/kernel/exec_tags.txt @@ -1,10 +1,8 @@ -fails:Kernel#exec raises an ArgumentError if the command includes a null byte fails:Kernel#exec raises Errno::EACCES when passed a directory fails:Kernel#exec with a single argument subjects the specified command to shell expansion fails:Kernel#exec with a command array uses the first element as the command name and the second as the argv[0] value fails:Kernel#exec with a command array coerces the argument using to_ary fails:Kernel#exec with a command array raises Argument error if the Array does not have exactly two elements -fails:Kernel.exec raises an ArgumentError if the command includes a null byte fails:Kernel.exec raises Errno::EACCES when passed a directory fails:Kernel.exec with a single argument subjects the specified command to shell expansion fails:Kernel.exec with a command array uses the first element as the command name and the second as the argv[0] value diff --git a/spec/tags/1.9/ruby/core/kernel/exec_tags.txt b/spec/tags/1.9/ruby/core/kernel/exec_tags.txt index 12077e1a4f2..2197b726b66 100644 --- a/spec/tags/1.9/ruby/core/kernel/exec_tags.txt +++ b/spec/tags/1.9/ruby/core/kernel/exec_tags.txt @@ -1,11 +1,9 @@ -fails:Kernel#exec raises an ArgumentError if the command includes a null byte fails:Kernel#exec raises Errno::EACCES when passed a directory fails:Kernel#exec unsets other environment variables when given a true :unsetenv_others option fails:Kernel#exec sets the current directory when given the :chdir option fails:Kernel#exec with a single argument subjects the specified command to shell expansion fails:Kernel#exec with a command array uses the first element as the command name and the second as the argv[0] value fails:Kernel#exec with a command array coerces the argument using to_ary -fails:Kernel.exec raises an ArgumentError if the command includes a null byte fails:Kernel.exec raises Errno::EACCES when passed a directory fails:Kernel.exec unsets other environment variables when given a true :unsetenv_others option fails:Kernel.exec sets the current directory when given the :chdir option diff --git a/src/org/jruby/RubyKernel.java b/src/org/jruby/RubyKernel.java index 2ba7d9f3b22..e3bd05923f2 100644 --- a/src/org/jruby/RubyKernel.java +++ b/src/org/jruby/RubyKernel.java @@ -1755,8 +1755,17 @@ public static IRubyObject _exec_internal(ThreadContext context, IRubyObject recv private static IRubyObject execCommon(Ruby runtime, IRubyObject env, IRubyObject prog, IRubyObject options, IRubyObject[] args) { // This is a fairly specific hack for empty string, but it does the job - if (args.length == 1 && args[0].convertToString().isEmpty()) { - throw runtime.newErrnoENOENTError(args[0].convertToString().toString()); + if (args.length == 1) { + RubyString command = args[0].convertToString(); + if (command.isEmpty()) { + throw runtime.newErrnoENOENTError(command.toString()); + } else { + for(byte b : command.getBytes()) { + if (b == 0x00) { + throw runtime.newArgumentError("string contains null byte"); + } + } + } } ThreadContext context = runtime.getCurrentContext();