Skip to content

Commit d503381

Browse files
committed
hash.c: cut off if recursion
* hash.c (rb_hash): cut off if recursion detected to get rid of stack overflow. [ruby-core:58567] [Bug ruby#9151] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent a4b0c3c commit d503381

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Tue Nov 26 22:43:36 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* hash.c (rb_hash): cut off if recursion detected to get rid of stack
4+
overflow. [ruby-core:58567] [Bug #9151]
5+
16
Tue Nov 26 20:02:39 2013 Koichi Sasada <ko1@atdot.net>
27

38
* test/ruby/test_settracefunc.rb: add tests for a_call/a_return

hash.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,17 @@ rb_any_cmp(VALUE a, VALUE b)
7676
return !rb_eql(a, b);
7777
}
7878

79+
static VALUE
80+
hash_recursive(VALUE obj, VALUE arg, int recurse)
81+
{
82+
if (recurse) return INT2FIX(0);
83+
return rb_funcallv(obj, id_hash, 0, 0);
84+
}
85+
7986
VALUE
8087
rb_hash(VALUE obj)
8188
{
82-
VALUE hval = rb_funcall(obj, id_hash, 0);
89+
VALUE hval = rb_exec_recursive(hash_recursive, obj, 0);
8390
retry:
8491
switch (TYPE(hval)) {
8592
case T_FIXNUM:

test/ruby/test_hash.rb

+14
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,20 @@ def test_inverse_hash
10601060
end
10611061
end
10621062

1063+
def test_recursive_hash_value
1064+
bug9151 = '[ruby-core:58567] [Bug #9151]'
1065+
1066+
s = Struct.new(:x) {def hash; [x,""].hash; end}
1067+
a = s.new
1068+
b = s.new
1069+
a.x = b
1070+
b.x = a
1071+
ah = assert_nothing_raised(SystemStackError, bug9151) {a.hash}
1072+
bh = assert_nothing_raised(SystemStackError, bug9151) {b.hash}
1073+
assert_equal(ah, bh, bug9151)
1074+
assert_not_equal([a,"hello"].hash, [b,"world"].hash, bug9151)
1075+
end
1076+
10631077
class TestSubHash < TestHash
10641078
class SubHash < Hash
10651079
end

0 commit comments

Comments
 (0)