diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-14 05:45:19 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-14 05:45:19 +0000 |
commit | 90d94de3b00f506dfb769501a5f11dbf2186d434 (patch) | |
tree | 7ab10f6d5d87988cd1208c597b9737c73e1ac073 /test | |
parent | 1bb654d307ea0b536123514ddc459f37e369076e (diff) |
merge revision(s) 43942,43957,43975: [Backport #9187]
* hash.c (rb_hash_rehash): fix to free new st_table when exception
is raised in do_hash(). [Bug #9187]
* hash.c (rb_hash_rehash): make temporary st_table under the control
of GC. [Bug #9187]
* test/ruby/test_hash.rb: add a test for above.
* array.c (rb_hash_rehash): use hash_alloc() instead of rb_hash_new().
[Bug #9187]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@44938 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/envutil.rb | 25 | ||||
-rw-r--r-- | test/ruby/test_hash.rb | 29 |
2 files changed, 54 insertions, 0 deletions
diff --git a/test/ruby/envutil.rb b/test/ruby/envutil.rb index 73f0101ac1..6998842259 100644 --- a/test/ruby/envutil.rb +++ b/test/ruby/envutil.rb @@ -184,6 +184,31 @@ module Test assert(msg === stderr, "warning message #{stderr.inspect} is expected to match #{msg.inspect}") end + def assert_no_memory_leak(args, prepare, code, message=nil, opt = {}) + limit = opt.delete(:limit) || 1.5 + token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m" + token_dump = token.dump + token_re = Regexp.quote(token) + envs = args.shift if Array === args and Hash === args.first + args = [ + "--disable=gems", + "-r", File.expand_path("../memory_status", __FILE__), + *args, + "-v", "-", + ] + args.unshift(envs) if envs + cmd = [ + 'END {STDERR.puts '"#{token_dump}"'"FINAL=#{Memory::Status.new.size}"}', + prepare, + 'STDERR.puts('"#{token_dump}"'"START=#{$initial_size = Memory::Status.new.size}")', + code, + ].join("\n") + _, err, status = EnvUtil.invoke_ruby(args, cmd, true, true, opt) + before = err.sub!(/^#{token_re}START=(\d+)\n/, '') && $1.to_i + after = err.sub!(/^#{token_re}FINAL=(\d+)\n/, '') && $1.to_i + assert_equal([true, ""], [status.success?, err], message) + assert_operator(after.fdiv(before), :<, limit, message) + end def assert_is_minus_zero(f) assert(1.0/f == -Float::INFINITY, "#{f} is not -0.0") diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 82272cb301..af3fa78e0a 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1,5 +1,6 @@ require 'test/unit' require 'continuation' +require_relative "envutil" class TestHash < Test::Unit::TestCase @@ -920,4 +921,32 @@ class TestHash < Test::Unit::TestCase assert_not_equal(h.hash, h.invert.hash, feature4262) end end + + def test_exception_in_rehash + bug9187 = '[ruby-core:58728] [Bug #9187]' + + prepare = <<-EOS + class Foo + def initialize + @raise = false + end + + def hash + raise if @raise + @raise = true + return 0 + end + end + EOS + + code = <<-EOS + h = {Foo.new => true} + 10_0000.times do + h.rehash rescue nil + end + GC.start + EOS + + assert_no_memory_leak([], prepare, code, bug9187) + end end |