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 #9151]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-11-26 13:43:40 +00:00
parent a4b0c3c26b
commit d503381ce8
3 changed files with 27 additions and 1 deletions

View File

@ -1,3 +1,8 @@
Tue Nov 26 22:43:36 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* hash.c (rb_hash): cut off if recursion detected to get rid of stack
overflow. [ruby-core:58567] [Bug #9151]
Tue Nov 26 20:02:39 2013 Koichi Sasada <ko1@atdot.net>
* test/ruby/test_settracefunc.rb: add tests for a_call/a_return

9
hash.c
View File

@ -76,10 +76,17 @@ rb_any_cmp(VALUE a, VALUE b)
return !rb_eql(a, b);
}
static VALUE
hash_recursive(VALUE obj, VALUE arg, int recurse)
{
if (recurse) return INT2FIX(0);
return rb_funcallv(obj, id_hash, 0, 0);
}
VALUE
rb_hash(VALUE obj)
{
VALUE hval = rb_funcall(obj, id_hash, 0);
VALUE hval = rb_exec_recursive(hash_recursive, obj, 0);
retry:
switch (TYPE(hval)) {
case T_FIXNUM:

View File

@ -1060,6 +1060,20 @@ class TestHash < Test::Unit::TestCase
end
end
def test_recursive_hash_value
bug9151 = '[ruby-core:58567] [Bug #9151]'
s = Struct.new(:x) {def hash; [x,""].hash; end}
a = s.new
b = s.new
a.x = b
b.x = a
ah = assert_nothing_raised(SystemStackError, bug9151) {a.hash}
bh = assert_nothing_raised(SystemStackError, bug9151) {b.hash}
assert_equal(ah, bh, bug9151)
assert_not_equal([a,"hello"].hash, [b,"world"].hash, bug9151)
end
class TestSubHash < TestHash
class SubHash < Hash
end