From 886142e8eecec4956b4f5d1d3fa8761dba1cd7d1 Mon Sep 17 00:00:00 2001 From: nagachika Date: Sun, 16 Feb 2014 16:47:24 +0000 Subject: merge revision(s) r43942,r43957,r43975: [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_2_0_0@45011 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 17 +++++++++++++++++ hash.c | 10 ++++++++++ test/ruby/test_hash.rb | 28 ++++++++++++++++++++++++++++ version.h | 2 +- 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 55b4843d67..feefdefbda 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +Mon Feb 17 01:41:59 2014 Masaki Matsushita + + * array.c (rb_hash_rehash): use hash_alloc() instead of rb_hash_new(). + [Bug #9187] + +Mon Feb 17 01:41:59 2014 Masaki Matsushita + + * 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. + +Mon Feb 17 01:41:59 2014 Masaki Matsushita + + * hash.c (rb_hash_rehash): fix to free new st_table when exception + is raised in do_hash(). [Bug #9187] + Mon Feb 17 01:18:04 2014 Nobuyoshi Nakada * eval.c (rb_mod_s_constants): return its own constants for other diff --git a/hash.c b/hash.c index 58fe8c346d..68f978081c 100644 --- a/hash.c +++ b/hash.c @@ -480,6 +480,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) { @@ -512,6 +517,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_ITER_LEV(hash) > 0) { @@ -520,10 +526,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/test_hash.rb b/test/ruby/test_hash.rb index c84ee74adf..a77c5cf43f 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -993,6 +993,34 @@ class TestHash < Test::Unit::TestCase 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 + class TestSubHash < TestHash class SubHash < Hash end diff --git a/version.h b/version.h index ec116411af..71b7294948 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.0.0" #define RUBY_RELEASE_DATE "2014-02-17" -#define RUBY_PATCHLEVEL 429 +#define RUBY_PATCHLEVEL 430 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 2 -- cgit v1.2.3