Skip to content

Exception NegativeArraySizeException during JSON.dump of large Hash #6265

@jbaiza

Description

@jbaiza

Environment Information

Provide at least:

  • JRuby version (jruby -v) and command line (flags, JRUBY_OPTS, etc)
    Originally detected on 9.2.9.0:
    jruby 9.2.9.0 (2.5.7) 2019-10-30 458ad3e OpenJDK 64-Bit Server VM 11.0.5+10 on 11.0.5+10 [darwin-x86_64]
    but can be reproduced also on the latest 9.2.11.1:
    jruby 9.2.11.1 (2.5.7) 2020-03-25 b1f55b1 OpenJDK 64-Bit Server VM 11.0.5+10 on 11.0.5+10 [darwin-x86_64]
  • Operating system and platform (e.g. uname -a)
    Linux staging-app1 4.4.0-170-generic Allow the RubyClass to be determined when extending BigDecimal #199-Ubuntu SMP Thu Nov 14 01:45:04 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
    also on Dev box:
    Darwin JBA-MacBook-Pro.local 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar 4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64

Other relevant info you may wish to add:

  • Installed or activated gems - N/A
  • Application/framework version (e.g. Rails, Sinatra) - N/A
  • Environment variables - increase Java memory with JAVA_OPTS=-Xmx4g

Expected Behavior

  • Describe your expectation of how JRuby should behave, perhaps by showing how CRuby/MRI behaves.
    JSON.dump completes without an error for a large hash. On MRI 2.5.1 provided sample code completes without an error.
  • Provide an executable Ruby script or a link to an example repository.
require 'JSON'
arr = [{"0" => "0", "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5"}]
begin
  25.times do
    arr.concat arr
    puts "ARR size: #{arr.size}"
  end
  puts "JSON.dump size: #{JSON.dump(arr).size}"
  arr.concat arr;true
  arr.size
  puts "ARR size: #{arr.size}"
  puts "JSON.dump size: #{JSON.dump(arr).size}"
end

Actual Behavior

An error is thrown:

Traceback (most recent call last):
       16: from json.ext.Generator$Handler.generateNew(Generator.java:194)
       15: from json.ext.Generator$4.generate(Generator.java:272)
       14: from json.ext.Generator$4.generate(Generator.java:315)
       13: from json.ext.Generator$5.generate(Generator.java:329)
       12: from json.ext.Generator$5.generate(Generator.java:356)
       11: from org.jruby.dist/org.jruby.RubyHash.visitAll(RubyHash.java:2746)
       10: from org.jruby.dist/org.jruby.RubyHash.visitLimited(RubyHash.java:699)
        9: from org.jruby.dist/org.jruby.RubyHash$Visitor.visit(RubyHash.java:677)
        8: from json.ext.Generator$5$1.visit(Generator.java:377)
        7: from json.ext.Generator$6.generate(Generator.java:391)
        6: from json.ext.Generator$6.generate(Generator.java:412)
        5: from json.ext.StringEncoder.encode(StringEncoder.java:51)
        4: from json.ext.ByteListTranscoder.quoteStop(ByteListTranscoder.java:147)
        3: from org.jruby.dist/org.jruby.util.ByteList.append(ByteList.java:530)
        2: from org.jruby.dist/org.jruby.util.ByteList.append(ByteList.java:546)
        1: from org.jruby.dist/org.jruby.util.ByteList.grow(ByteList.java:1107)
Java::JavaLang::NegativeArraySizeException (-1746927586)

It seems that 2Gb is the limit when the error starts to occur.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions