From 94f01c55dfd2d1ce49fb253432267d87cca64cd1 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 26 Nov 2013 17:20:16 +0000 Subject: [PATCH] should not ignore the rest of recursive constructs * array.c (rb_ary_hash): should not ignore the rest of recursive constructs. * hash.c (rb_hash_hash): ditto. * range.c (range_hash): ditto. * struct.c (rb_struct_hash): ditto. * test/-ext-/test_recursion.rb (TestRecursion): separate from test/ruby/test_thread.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 14 +++++++++++++ array.c | 2 +- ext/-test-/recursion/extconf.rb | 2 ++ ext/-test-/recursion/recursion.c | 28 +++++++++++++++++++++++++ hash.c | 2 +- range.c | 2 +- struct.c | 2 +- test/-ext-/test_recursion.rb | 36 ++++++++++++++++++++++++++++++++ test/ruby/test_array.rb | 2 +- test/ruby/test_thread.rb | 13 ------------ 10 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 ext/-test-/recursion/extconf.rb create mode 100644 ext/-test-/recursion/recursion.c create mode 100644 test/-ext-/test_recursion.rb diff --git a/ChangeLog b/ChangeLog index 7ab05f951b..b0d5acd3ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Wed Nov 27 02:20:13 2013 Nobuyoshi Nakada + + * array.c (rb_ary_hash): should not ignore the rest of recursive + constructs. + + * hash.c (rb_hash_hash): ditto. + + * range.c (range_hash): ditto. + + * struct.c (rb_struct_hash): ditto. + + * test/-ext-/test_recursion.rb (TestRecursion): separate from + test/ruby/test_thread.rb. + Tue Nov 26 22:43:36 2013 Nobuyoshi Nakada * hash.c (rb_hash): cut off if recursion detected to get rid of stack diff --git a/array.c b/array.c index baf6118a18..b27ae299f4 100644 --- a/array.c +++ b/array.c @@ -3807,7 +3807,7 @@ recursive_hash(VALUE ary, VALUE dummy, int recur) static VALUE rb_ary_hash(VALUE ary) { - return rb_exec_recursive_outer(recursive_hash, ary, 0); + return rb_exec_recursive_paired(recursive_hash, ary, ary, 0); } /* diff --git a/ext/-test-/recursion/extconf.rb b/ext/-test-/recursion/extconf.rb new file mode 100644 index 0000000000..92b28657f0 --- /dev/null +++ b/ext/-test-/recursion/extconf.rb @@ -0,0 +1,2 @@ +require 'mkmf' +create_makefile("-test-/recursion") diff --git a/ext/-test-/recursion/recursion.c b/ext/-test-/recursion/recursion.c new file mode 100644 index 0000000000..13d41f0ba8 --- /dev/null +++ b/ext/-test-/recursion/recursion.c @@ -0,0 +1,28 @@ +#include + +static VALUE +recursive_i(VALUE obj, VALUE mid, int recur) +{ + if (recur) return Qnil; + return rb_funcallv(obj, rb_to_id(mid), 0, 0); +} + +static VALUE +exec_recursive(VALUE self, VALUE mid) +{ + return rb_exec_recursive(recursive_i, self, mid); +} + +static VALUE +exec_recursive_outer(VALUE self, VALUE mid) +{ + return rb_exec_recursive_outer(recursive_i, self, mid); +} + +void +Init_recursion(void) +{ + VALUE m = rb_define_module_under(rb_define_module("Bug"), "Recursive"); + rb_define_method(m, "exec_recursive", exec_recursive, 1); + rb_define_method(m, "exec_recursive_outer", exec_recursive_outer, 1); +} diff --git a/hash.c b/hash.c index 1694a031c0..9944b5f4b1 100644 --- a/hash.c +++ b/hash.c @@ -1944,7 +1944,7 @@ recursive_hash(VALUE hash, VALUE dummy, int recur) static VALUE rb_hash_hash(VALUE hash) { - return rb_exec_recursive_outer(recursive_hash, hash, 0); + return rb_exec_recursive_paired(recursive_hash, hash, hash, 0); } static int diff --git a/range.c b/range.c index ebd0811a98..c772347c37 100644 --- a/range.c +++ b/range.c @@ -274,7 +274,7 @@ recursive_hash(VALUE range, VALUE dummy, int recur) static VALUE range_hash(VALUE range) { - return rb_exec_recursive_outer(recursive_hash, range, 0); + return rb_exec_recursive_paired(recursive_hash, range, range, 0); } static void diff --git a/struct.c b/struct.c index a961ac8600..bad339ffbb 100644 --- a/struct.c +++ b/struct.c @@ -980,7 +980,7 @@ recursive_hash(VALUE s, VALUE dummy, int recur) static VALUE rb_struct_hash(VALUE s) { - return rb_exec_recursive_outer(recursive_hash, s, 0); + return rb_exec_recursive_paired(recursive_hash, s, s, 0); } static VALUE diff --git a/test/-ext-/test_recursion.rb b/test/-ext-/test_recursion.rb new file mode 100644 index 0000000000..43a256f942 --- /dev/null +++ b/test/-ext-/test_recursion.rb @@ -0,0 +1,36 @@ +# -*- coding: us-ascii -*- +require 'test/unit' +require_relative '../ruby/envutil' + +class TestRecursion < Test::Unit::TestCase + require '-test-/recursion' + + def setup + @obj = Struct.new(:visited).new(false) + @obj.extend(Bug::Recursive) + end + + def test_recursive + def @obj.doit + self.visited = true + exec_recursive(:doit) + raise "recursive" + end + assert_raise_with_message(RuntimeError, "recursive") { + @obj.exec_recursive(:doit) + } + assert(@obj.visited, "obj.hash was not called") + end + + def test_recursive_outer + def @obj.doit + self.visited = true + exec_recursive_outer(:doit) + raise "recursive_outer should short circuit intermediate calls" + end + assert_nothing_raised { + @obj.exec_recursive_outer(:doit) + } + assert(@obj.visited, "obj.hash was not called") + end +end diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index d7e1c365dd..9411c6a088 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -2012,9 +2012,9 @@ class TestArray < Test::Unit::TestCase end def test_hash2 + assert_not_equal([[1]].hash, [[2]].hash) a = [] a << a - assert_equal([[a]].hash, a.hash) assert_not_equal([a, a].hash, a.hash) # Implementation dependent end diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb index ab1b81a8fe..f74c4ec9cb 100644 --- a/test/ruby/test_thread.rb +++ b/test/ruby/test_thread.rb @@ -467,19 +467,6 @@ class TestThread < Test::Unit::TestCase m.unlock end - def test_recursive_outer - arr = [] - obj = Struct.new(:foo, :visited).new(arr, false) - arr << obj - def obj.hash - self[:visited] = true - super - raise "recursive_outer should short circuit intermediate calls" - end - assert_nothing_raised {arr.hash} - assert(obj[:visited], "obj.hash was not called") - end - def test_thread_instance_variable bug4389 = '[ruby-core:35192]' assert_in_out_err([], <<-INPUT, %w(), [], bug4389)