[前][次][番号順一覧][スレッド一覧]

ruby-changes:32859

From: usa <ko1@a...>
Date: Fri, 14 Feb 2014 14:45:26 +0900 (JST)
Subject: [ruby-changes:32859] usa:r44938 (ruby_1_9_3): merge revision(s) 43942, 43957, 43975: [Backport #9187]

usa	2014-02-14 14:45:19 +0900 (Fri, 14 Feb 2014)

  New Revision: 44938

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44938

  Log:
    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]

  Modified directories:
    branches/ruby_1_9_3/
  Modified files:
    branches/ruby_1_9_3/ChangeLog
    branches/ruby_1_9_3/hash.c
    branches/ruby_1_9_3/test/ruby/envutil.rb
    branches/ruby_1_9_3/test/ruby/test_hash.rb
    branches/ruby_1_9_3/version.h
Index: ruby_1_9_3/ChangeLog
===================================================================
--- ruby_1_9_3/ChangeLog	(revision 44937)
+++ ruby_1_9_3/ChangeLog	(revision 44938)
@@ -1,3 +1,20 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ChangeLog#L1
+Fri Feb 14 14:40:27 2014  Masaki Matsushita  <glass.saga@g...>
+
+	* 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@g...>
+
+	* 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@g...>
+
+	* 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@s...>
 
 	* lib/optparse.rb:  The Integer acceptable now allows binary and
Index: ruby_1_9_3/hash.c
===================================================================
--- ruby_1_9_3/hash.c	(revision 44937)
+++ ruby_1_9_3/hash.c	(revision 44938)
@@ -442,6 +442,11 @@ rb_hash_s_try_convert(VALUE dummy, VALUE https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/hash.c#L442
     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, https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/hash.c#L479
 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) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/hash.c#L488
     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;
 }
Index: ruby_1_9_3/version.h
===================================================================
--- ruby_1_9_3/version.h	(revision 44937)
+++ ruby_1_9_3/version.h	(revision 44938)
@@ -1,5 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/version.h#L1
 #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
Index: ruby_1_9_3/test/ruby/test_hash.rb
===================================================================
--- ruby_1_9_3/test/ruby/test_hash.rb	(revision 44937)
+++ ruby_1_9_3/test/ruby/test_hash.rb	(revision 44938)
@@ -1,5 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ruby/test_hash.rb#L1
 require 'test/unit'
 require 'continuation'
+require_relative "envutil"
 
 class TestHash < Test::Unit::TestCase
 
@@ -920,4 +921,32 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ruby/test_hash.rb#L921
       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
Index: ruby_1_9_3/test/ruby/envutil.rb
===================================================================
--- ruby_1_9_3/test/ruby/envutil.rb	(revision 44937)
+++ ruby_1_9_3/test/ruby/envutil.rb	(revision 44938)
@@ -184,6 +184,31 @@ module Test https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/ruby/envutil.rb#L184
         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")

Property changes on: ruby_1_9_3
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r43942,43957,43975


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]