diff options
-rw-r--r-- | ChangeLog | 17 | ||||
-rw-r--r-- | hash.c | 10 | ||||
-rw-r--r-- | test/ruby/envutil.rb | 25 | ||||
-rw-r--r-- | test/ruby/test_hash.rb | 29 | ||||
-rw-r--r-- | version.h | 2 |
5 files changed, 82 insertions, 1 deletions
@@ -1,3 +1,20 @@ +Fri Feb 14 14:40:27 2014 Masaki Matsushita <glass.saga@gmail.com> + + * array.c (rb_hash_rehash): use hash_alloc() instead of rb_hash_new(). + [Bug #9187] + +Fri Feb 14 14:40:27 2014 Masaki Matsushita <glass.saga@gmail.com> + + * 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. + +Fri Feb 14 14:40:27 2014 Masaki Matsushita <glass.saga@gmail.com> + + * hash.c (rb_hash_rehash): fix to free new st_table when exception + is raised in do_hash(). [Bug #9187] + Fri Feb 14 13:51:45 2014 Eric Hodel <drbrain@segment7.net> * lib/optparse.rb: The Integer acceptable now allows binary and @@ -442,6 +442,11 @@ rb_hash_s_try_convert(VALUE dummy, VALUE hash) return rb_check_hash_type(hash); } +struct rehash_arg { + VALUE hash; + st_table *tbl; +}; + static int rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg) { @@ -474,6 +479,7 @@ rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg) static VALUE rb_hash_rehash(VALUE hash) { + VALUE tmp; st_table *tbl; if (RHASH(hash)->iter_lev > 0) { @@ -482,10 +488,14 @@ rb_hash_rehash(VALUE hash) rb_hash_modify_check(hash); if (!RHASH(hash)->ntbl) return hash; + tmp = hash_alloc(0); tbl = st_init_table_with_size(RHASH(hash)->ntbl->type, RHASH(hash)->ntbl->num_entries); + RHASH(tmp)->ntbl = tbl; + rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tbl); st_free_table(RHASH(hash)->ntbl); RHASH(hash)->ntbl = tbl; + RHASH(tmp)->ntbl = 0; return hash; } 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 @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 519 +#define RUBY_PATCHLEVEL 520 #define RUBY_RELEASE_DATE "2014-02-14" #define RUBY_RELEASE_YEAR 2014 |