Skip to content

Commit 9c66947

Browse files
committed
merge revision(s) 43942,43957,43975: [Backport ruby#9187]
* hash.c (rb_hash_rehash): fix to free new st_table when exception is raised in do_hash(). [Bug ruby#9187] * hash.c (rb_hash_rehash): make temporary st_table under the control of GC. [Bug ruby#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 ruby#9187] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@44938 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent f938707 commit 9c66947

5 files changed

Lines changed: 82 additions & 1 deletion

File tree

ChangeLog

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
Fri Feb 14 14:40:27 2014 Masaki Matsushita <glass.saga@gmail.com>
2+
3+
* array.c (rb_hash_rehash): use hash_alloc() instead of rb_hash_new().
4+
[Bug #9187]
5+
6+
Fri Feb 14 14:40:27 2014 Masaki Matsushita <glass.saga@gmail.com>
7+
8+
* hash.c (rb_hash_rehash): make temporary st_table under the control
9+
of GC. [Bug #9187]
10+
11+
* test/ruby/test_hash.rb: add a test for above.
12+
13+
Fri Feb 14 14:40:27 2014 Masaki Matsushita <glass.saga@gmail.com>
14+
15+
* hash.c (rb_hash_rehash): fix to free new st_table when exception
16+
is raised in do_hash(). [Bug #9187]
17+
118
Fri Feb 14 13:51:45 2014 Eric Hodel <drbrain@segment7.net>
219

320
* lib/optparse.rb: The Integer acceptable now allows binary and

hash.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ rb_hash_s_try_convert(VALUE dummy, VALUE hash)
442442
return rb_check_hash_type(hash);
443443
}
444444

445+
struct rehash_arg {
446+
VALUE hash;
447+
st_table *tbl;
448+
};
449+
445450
static int
446451
rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
447452
{
@@ -474,6 +479,7 @@ rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
474479
static VALUE
475480
rb_hash_rehash(VALUE hash)
476481
{
482+
VALUE tmp;
477483
st_table *tbl;
478484

479485
if (RHASH(hash)->iter_lev > 0) {
@@ -482,10 +488,14 @@ rb_hash_rehash(VALUE hash)
482488
rb_hash_modify_check(hash);
483489
if (!RHASH(hash)->ntbl)
484490
return hash;
491+
tmp = hash_alloc(0);
485492
tbl = st_init_table_with_size(RHASH(hash)->ntbl->type, RHASH(hash)->ntbl->num_entries);
493+
RHASH(tmp)->ntbl = tbl;
494+
486495
rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tbl);
487496
st_free_table(RHASH(hash)->ntbl);
488497
RHASH(hash)->ntbl = tbl;
498+
RHASH(tmp)->ntbl = 0;
489499

490500
return hash;
491501
}

test/ruby/envutil.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,31 @@ def assert_warn(msg)
184184
assert(msg === stderr, "warning message #{stderr.inspect} is expected to match #{msg.inspect}")
185185
end
186186

187+
def assert_no_memory_leak(args, prepare, code, message=nil, opt = {})
188+
limit = opt.delete(:limit) || 1.5
189+
token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m"
190+
token_dump = token.dump
191+
token_re = Regexp.quote(token)
192+
envs = args.shift if Array === args and Hash === args.first
193+
args = [
194+
"--disable=gems",
195+
"-r", File.expand_path("../memory_status", __FILE__),
196+
*args,
197+
"-v", "-",
198+
]
199+
args.unshift(envs) if envs
200+
cmd = [
201+
'END {STDERR.puts '"#{token_dump}"'"FINAL=#{Memory::Status.new.size}"}',
202+
prepare,
203+
'STDERR.puts('"#{token_dump}"'"START=#{$initial_size = Memory::Status.new.size}")',
204+
code,
205+
].join("\n")
206+
_, err, status = EnvUtil.invoke_ruby(args, cmd, true, true, opt)
207+
before = err.sub!(/^#{token_re}START=(\d+)\n/, '') && $1.to_i
208+
after = err.sub!(/^#{token_re}FINAL=(\d+)\n/, '') && $1.to_i
209+
assert_equal([true, ""], [status.success?, err], message)
210+
assert_operator(after.fdiv(before), :<, limit, message)
211+
end
187212

188213
def assert_is_minus_zero(f)
189214
assert(1.0/f == -Float::INFINITY, "#{f} is not -0.0")

test/ruby/test_hash.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'test/unit'
22
require 'continuation'
3+
require_relative "envutil"
34

45
class TestHash < Test::Unit::TestCase
56

@@ -920,4 +921,32 @@ def test_inverse_hash
920921
assert_not_equal(h.hash, h.invert.hash, feature4262)
921922
end
922923
end
924+
925+
def test_exception_in_rehash
926+
bug9187 = '[ruby-core:58728] [Bug #9187]'
927+
928+
prepare = <<-EOS
929+
class Foo
930+
def initialize
931+
@raise = false
932+
end
933+
934+
def hash
935+
raise if @raise
936+
@raise = true
937+
return 0
938+
end
939+
end
940+
EOS
941+
942+
code = <<-EOS
943+
h = {Foo.new => true}
944+
10_0000.times do
945+
h.rehash rescue nil
946+
end
947+
GC.start
948+
EOS
949+
950+
assert_no_memory_leak([], prepare, code, bug9187)
951+
end
923952
end

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define RUBY_VERSION "1.9.3"
2-
#define RUBY_PATCHLEVEL 519
2+
#define RUBY_PATCHLEVEL 520
33

44
#define RUBY_RELEASE_DATE "2014-02-14"
55
#define RUBY_RELEASE_YEAR 2014

0 commit comments

Comments
 (0)