Calling map on string causes java.lang.ArrayIndexOutOfBoundsException
---------------------------------------------------------------------

                 Key: JRUBY-1571
                 URL: http://jira.codehaus.org/browse/JRUBY-1571
             Project: JRuby
          Issue Type: Bug
          Components: Interpreter
    Affects Versions: JRuby 1.x
         Environment: OSX, JRuby from source (11/13/07)
            Reporter: David Koontz


This is a pretty strange one, I could not isolate it down into a smaller test 
case.  I ran into this when I found an old Ruby app I had written and wanted to 
compare run times between Ruby and JRuby.  The app ran fine under Ruby but 
under JRuby produces the following exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
        at java.lang.System.arraycopy(Native Method)
        at org.jruby.util.ByteList.append(ByteList.java:222)
        at org.jruby.util.ByteList.append(ByteList.java:213)
        at org.jruby.RubyString.op_plus(RubyString.java:360)
        at org.jruby.RubyStringInvoker$op_plus_method_1_0.call(Unknown Source)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:151)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:104)
        at string_permutations.closure1(string_permutations.rb:28)
        at string_permutationsBlockclosure1xx1.call(Unknown Source)
        at 
org.jruby.runtime.CompiledBlockLight.yield(CompiledBlockLight.java:124)
        at 
org.jruby.runtime.CompiledBlockLight.yield(CompiledBlockLight.java:105)
        at org.jruby.runtime.Block.yield(Block.java:111)
        at org.jruby.RubyEnumerable$9.call(RubyEnumerable.java:244)
        at org.jruby.runtime.CallBlock.yield(CallBlock.java:105)
        at org.jruby.runtime.CallBlock.yield(CallBlock.java:77)
        at org.jruby.runtime.Block.yield(Block.java:111)
        at org.jruby.RubyString.each_line(RubyString.java:3277)
        at org.jruby.RubyStringInvoker$each_line_method_0_1.call(Unknown Source)
        at org.jruby.RubyObject.callMethod(RubyObject.java:513)
        at org.jruby.RubyObject.callMethod(RubyObject.java:463)
        at org.jruby.RubyEnumerable.callEach(RubyEnumerable.java:61)
        at org.jruby.RubyEnumerable.collect(RubyEnumerable.java:242)
        at org.jruby.RubyEnumerableInvoker$collect_method_0_0.call(Unknown 
Source)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:151)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:110)
        at string_permutations.closure0(string_permutations.rb:28)
        at string_permutationsBlockclosure0xx1.call(Unknown Source)
        at org.jruby.runtime.CompiledBlock.yield(CompiledBlock.java:124)
        at org.jruby.runtime.CompiledBlock.yield(CompiledBlock.java:105)
        at org.jruby.runtime.Block.yield(Block.java:111)
        at org.jruby.RubyEnumerable$EachWithIndex.call(RubyEnumerable.java:307)
        at org.jruby.runtime.CallBlock.yield(CallBlock.java:105)
        at org.jruby.runtime.CallBlock.yield(CallBlock.java:77)
        at org.jruby.runtime.Block.yield(Block.java:111)
        at org.jruby.RubyArray.each(RubyArray.java:1173)
        at org.jruby.RubyArrayInvoker$each_method_0_0.call(Unknown Source)
        at org.jruby.RubyObject.callMethod(RubyObject.java:513)
        at org.jruby.RubyObject.callMethod(RubyObject.java:463)
        at org.jruby.RubyEnumerable.each_with_index(RubyEnumerable.java:315)
        at 
org.jruby.RubyEnumerableInvoker$each_with_index_method_0_0.call(Unknown Source)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:137)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:110)
        at string_permutations.permutate__0(string_permutations.rb:17)
        at string_permutationsInvokerpermutate__0Fixed1.call(Unknown Source)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:151)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:104)
        at string_permutations.closure0(string_permutations.rb:21)
        at string_permutationsBlockclosure0xx1.call(Unknown Source)
        at org.jruby.runtime.CompiledBlock.yield(CompiledBlock.java:124)
        at org.jruby.runtime.CompiledBlock.yield(CompiledBlock.java:105)
        at org.jruby.runtime.Block.yield(Block.java:111)
        at org.jruby.RubyEnumerable$EachWithIndex.call(RubyEnumerable.java:307)
        at org.jruby.runtime.CallBlock.yield(CallBlock.java:105)
        at org.jruby.runtime.CallBlock.yield(CallBlock.java:77)
        at org.jruby.runtime.Block.yield(Block.java:111)
        at org.jruby.RubyArray.each(RubyArray.java:1173)
        at org.jruby.RubyArrayInvoker$each_method_0_0.call(Unknown Source)
        at org.jruby.RubyObject.callMethod(RubyObject.java:513)
        at org.jruby.RubyObject.callMethod(RubyObject.java:463)
        at org.jruby.RubyEnumerable.each_with_index(RubyEnumerable.java:315)
        at 
org.jruby.RubyEnumerableInvoker$each_with_index_method_0_0.call(Unknown Source)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:151)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:110)
        at string_permutations.permutate__0(string_permutations.rb:17)
        at string_permutationsInvokerpermutate__0Fixed1.call(Unknown Source)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:151)
        at 
org.jruby.runtime.CallSite$InlineCachingCallSite.call(CallSite.java:104)
        at string_permutations.__file__(string_permutations.rb:41)
        at string_permutations.load(string_permutations.rb)
        at org.jruby.Ruby.runScript(Ruby.java:574)
        at org.jruby.Ruby.runNormally(Ruby.java:496)
        at org.jruby.Ruby.runFromMain(Ruby.java:419)
        at org.jruby.Main.runInterpreter(Main.java:227)
        at org.jruby.Main.runInterpreter(Main.java:183)
        at org.jruby.Main.run(Main.java:116)
        at org.jruby.Main.run(Main.java:93)
        at org.jruby.Main.main(Main.java:84)

Here is the program that produces the error, the line causing the problem is 
"retval << results.map { |e| l + e.to_s}".  I tried experimenting with that 
line.  If you do just "results.map { |e| e}" the exception is triggered 
although inspecting results shows that it is a string.  Trying "ab".map{|e| e} 
does not produce any sort of error.

def permutate(letters)
  #if there is only one element, return it
  return letters if letters.size == 1
  
  #if this has been memoized, return that
  return @memoized[letters] if @memoized[letters]
  
  #for each character in the string
  retval = []
  letters.split(//).each_with_index do |l,i|
    
    #remove the current character and permutate the rest
    results = if 0 == i
      permutate(letters[1..-1]) 
    elsif i == letters.size-1
      permutate(letters[0...-1])
    else
      permutate(letters[0...i] + letters[i+1..-1])
    end
    puts "letters: #{letters}, l: #{l}, results: #{results.inspect}"
    retval << results.map { |e| l + e.to_s}
  end
  @memoized[letters] = retval.flatten
end

@memoized = Hash.new

permutate("abc")


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to