summary refs log tree commit
path: root/tests/test_json_generate.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-08-09 01:29:43 +0000
committerEric Wong <normalperson@yhbt.net>2011-08-09 03:38:59 +0000
commit8a82bffe927e8928e304b9610a75ea07b0c4a798 (patch)
tree6dbdaedbe53418d271235bbb4778b44de4484224 /tests/test_json_generate.rb
parentf7f78896607b6f6226cdee4ae76de922d4583d32 (diff)
downloadruby-json-8a82bffe927e8928e304b9610a75ea07b0c4a798.tar.gz
ext/json/generator/generator.c: prevent GC of temporary strings
We need to guard temporary strings from being collected while we
append to the JSON buffer (which may allocate memory).  The
RSTRING_PAIR macro is dangerous since it preserves no pointer to
the original string VALUE, allowing GC to reap the object while
we're still using the (C) string pointer.

The included test case shows data corruption with large
Bignums without this fix.
Diffstat (limited to 'tests/test_json_generate.rb')
-rwxr-xr-xtests/test_json_generate.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb
index 9b0cff4..bc4e395 100755
--- a/tests/test_json_generate.rb
+++ b/tests/test_json_generate.rb
@@ -177,4 +177,17 @@ EOT
     assert_raises(JSON::NestingError) { ary.to_json(s) }
     assert_equal 19, s.depth
   end
+
+  def test_gc
+    bignum_too_long_to_embed_as_string = 1234567890123456789012345
+    expect = bignum_too_long_to_embed_as_string.to_s
+    stress, GC.stress = GC.stress, true
+
+    10.times do |i|
+      tmp = bignum_too_long_to_embed_as_string.to_json
+      assert_equal expect, tmp
+    end
+    ensure
+      GC.stress = stress
+  end if GC.respond_to?(:stress=)
 end