Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json/ext 1.5.0 on 32-bit ree-1.8.7 sometimes produces garbage output on Bignums #46

Closed
redterror opened this issue Oct 1, 2010 · 9 comments

Comments

@redterror
Copy link

Here's a sample program that exhibits the problem:

ree-1.8.7@jsonbug1 steinbergd@davedev:~/dev/timing-bug$ cat test1.rb
require 'rubygems'
require 'json/ext'

puts "JSON Version: #{JSON::VERSION}"
1000000.times do |i|
s = 12345678900000.to_json
puts s
end
ree-1.8.7@jsonbug1 steinbergd@davedev:/dev/timing-bug$ ruby test1.rb | sort | uniq -c
2 �ï¿5678900000
999461 12345678900000
1 �ï¿5678900000
533 ï¿5678900000
3 @ï¿5678900000
1 JSON Version: 1.5.0
ree-1.8.7@jsonbug1 steinbergd@davedev:
/dev/timing-bug$ uname -a

Linux davedev 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux

Also on pastebin: http://pastebin.com/fpxF5Tzd

Switching the test program to json/pure fixes it. In addition, monkey patching Bignum.to_s like so:

class Bignum
def to_json
return self.to_s
end
end

also seems to fix it.

@redterror
Copy link
Author

Forgot to mention - the problem doesn't seem to exist on 64-bit machines. Or maybe it does and we just need a much bigger number!

@gilltots
Copy link

gilltots commented Oct 1, 2010

we're seeing this in script/console in a rails 2.3.8 app on 32-bit machines.

slightly easier repro steps are to run this in script/console from a rails app:
$ script/console production
Loading production environment (Rails 2.3.8)
ree-1.8.7-2010.02 > 10000.times{|i| s = 1234567890.to_json; puts s unless s == "1234567890"}
?L(567890?'P
567890
`?e
567890
PS8
567890
S8
567890
??c
567890
=> 10000

@jfahrenkrug
Copy link

I see the same issue. I found out that it's connected to the garbage collector. See:

a = {:x => 1309471200} # that's a Bignum
10000.times {|i| puts i; JSON.parse(a.to_json)}

if you run this multiple times, it always creates corrupt bignum-json on iteration 1554. That was a clear indication for a GC problem. So I tried it and alas, this works:

GC.disable
a = {:x => 1309471200} # that's a Bignum
10000.times {|i| puts i; JSON.parse(a.to_json)}
GC.enable

So it looks as if the GC in REE 1.8.7 p330 is somehow corrupt?

@nocode
Copy link

nocode commented Aug 9, 2011

I've posted a fix for this at http://redmine.ruby-lang.org/issues/5173

@nocode
Copy link

nocode commented Aug 9, 2011

I ported my changes against Ruby to the standalone Gem repo.
You can also pull in my changes for the json repo directly:

git pull git://bogomips.org/ruby-json.git master

There's also a second patch in there found by nobu.

Patches individually downloadable at:

http://bogomips.org/ruby-json.git/patch/?id=8a82bffe927e8928e304b96
http://bogomips.org/ruby-json.git/patch/?id=40869aa9fc8ab194813b850

nobu had another bug in the aforementioned redmine ticket I don't have time to
fix right now.

@flori
Copy link
Member

flori commented Aug 29, 2011

I applied your patch, it's in master now.

@flori flori closed this as completed Aug 29, 2011
@nocode
Copy link

nocode commented Aug 29, 2011

flori reply@reply.github.com wrote:

I applied your patch, it's in master now.

Thanks, I'd really like to get this in the next Ruby 1.9.3 RC, too.

@nurse
Copy link
Member

nurse commented Aug 30, 2011

I merged 2149f41 in r33122 (trunk) and r33123 (ruby_1_9_3), thanks!
So this will be included in 1.9.3.

@jfahrenkrug
Copy link

Great, thanks!

jsonn pushed a commit to jsonn/pkgsrc that referenced this issue Oct 11, 2014
2011-08-31 (1.5.4)
  * Fix memory leak when used from multiple JRuby. (Patch by
    jfirebaugh@github).
  * Apply patch by Eric Wong <nocode@yhbt.net> that fixes garbage collection
    problem reported in ruby/json#46.
  * Add :quirks_mode option to parser and generator.
  * Add support for Rational and Complex number additions via json/add/complex
    and json/add/rational requires.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants