JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-6820

Unexpected behavior of IO.popen when giving arguments as an array containing strings

    Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.7.0.pre1
    • Fix Version/s: JRuby 1.7.0.pre2
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      In IO#popen, to bypass the shell for avoiding messy shell escaping and potential security hole, giving command-line as an array containing string objects is available since Ruby 1.9. However, on JRuby, when passing arguments with shell special characters to IO#popen, whole arguments seem to be omitted.

      $ jruby -v -e 'p IO.popen(["/bin/echo", "(a)", "*" ], "r+") { |io| io.read }'
      jruby 1.7.0.preview1 (ruby-1.9.3-p203) (2012-05-19 00c8c98) (OpenJDK 64-Bit Server VM 1.6.0_20) [linux-amd64-java]
      "\n"
      

      In MRI, the /bin/echo works correctly.

      $ ruby -v -e 'p IO.popen(["/bin/echo", "(a)", "*" ], "r+") { |io| io.read }'
      ruby 1.9.3p253 (2012-07-04) [x86_64-linux]
      "(a) *\n"
      

        Activity

        Hide
        Charles Oliver Nutter added a comment -

        Still broken on master, and probably won't be fixed for 1.7pre2.

        Show
        Charles Oliver Nutter added a comment - Still broken on master, and probably won't be fixed for 1.7pre2.
        Hide
        Thomas E Enebo added a comment -

        Fixed in commit 1c3c16a.

        Show
        Thomas E Enebo added a comment - Fixed in commit 1c3c16a.
        Hide
        LanceMa added a comment - - edited

        I still reproduced this problem in JRuby 1.7.0.pre2, jdk7 1.7.0(32bit), win7 x64. This issue still exists. I tried several ways to let you know what exactly caused this issue.

        At first, my code is
        IO.popen('cmd1', 'op1', path+"#head")

        {|f| }

        #empty block does not work.
        which returned immediately.

        Then, I just put a statement in that empty block:
        IO.popen('cmd1', 'op1', path+"#head")

        {|f| puts f.gets }

        #block is not empty, then it works well.
        which works as expected.

        So it seems that a empty block does not make sense for IO.popen, otherwise I don't know how to explain that.

        Show
        LanceMa added a comment - - edited I still reproduced this problem in JRuby 1.7.0.pre2, jdk7 1.7.0(32bit), win7 x64. This issue still exists. I tried several ways to let you know what exactly caused this issue. At first, my code is IO.popen( 'cmd1', 'op1', path+"#head" ) {|f| } #empty block does not work. which returned immediately. Then, I just put a statement in that empty block: IO.popen( 'cmd1', 'op1', path+"#head" ) {|f| puts f.gets } #block is not empty, then it works well. which works as expected. So it seems that a empty block does not make sense for IO.popen, otherwise I don't know how to explain that.
        Hide
        Thomas E Enebo added a comment -

        LanceMa, Can you open this as a new issue? This does not appear to be related to this issue. It almost sounds like without any activity on the IO popened nothing happens. This issue was about the array form of popen was getting passed to a shell instead of invoked directly.

        Could you also try and provide some command-line we can run? I do not think what you provided as example usage would actually execute with popen on any impl (not having those args wrapped in a [] is probably the mistake?).

        Show
        Thomas E Enebo added a comment - LanceMa, Can you open this as a new issue? This does not appear to be related to this issue. It almost sounds like without any activity on the IO popened nothing happens. This issue was about the array form of popen was getting passed to a shell instead of invoked directly. Could you also try and provide some command-line we can run? I do not think what you provided as example usage would actually execute with popen on any impl (not having those args wrapped in a [] is probably the mistake?).
        Hide
        LanceMa added a comment -

        I'm sorry that I misspelled that []. However, I indeed used the right form with arguments wrapped by [].
        Now I provided another case which might be tested in another environment.

        First, I create a ruby file named testtime.rb and put it at folder c:\apps\test\ruby. Its content is :
        require 'date'
        File.open('c:\a.txt','w+'){|file| file << "test #

        {DateTime.now}

        "}

        Seconde, I run a shell in cmd of windows:
        jruby -e"IO.popen(['ruby','c:\apps\test\ruby\testtime.rb'])

        {|f| }

        "

        As you can see, no file named a.txt in c:\ is created.

        But if you modify the shell above like below:
        jruby -e"IO.popen(['ruby','c:\apps\test\ruby\testtime.rb'])

        {|f| puts f.gets}

        "

        You will get expected result: c:\a.txt is created.

        Show
        LanceMa added a comment - I'm sorry that I misspelled that []. However, I indeed used the right form with arguments wrapped by []. Now I provided another case which might be tested in another environment. First, I create a ruby file named testtime.rb and put it at folder c:\apps\test\ruby. Its content is : require 'date' File.open('c:\a.txt','w+'){|file| file << "test # {DateTime.now} "} Seconde, I run a shell in cmd of windows: jruby -e"IO.popen( ['ruby','c:\apps\test\ruby\testtime.rb'] ) {|f| } " As you can see, no file named a.txt in c:\ is created. But if you modify the shell above like below: jruby -e"IO.popen( ['ruby','c:\apps\test\ruby\testtime.rb'] ) {|f| puts f.gets} " You will get expected result: c:\a.txt is created.

          People

          • Assignee:
            Thomas E Enebo
            Reporter:
            Naohisa Goto
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: