aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-05-22 03:40:57 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-05-22 03:40:57 +0000
commitfeead4dca593de620a555dff4f7884a891ba5b67 (patch)
treedba0a9b6e8e3a370a436dca9c850123185eb1b81
parenta85b247171373d079d98eeef42c6014980062609 (diff)
downloadruby-feead4dca593de620a555dff4f7884a891ba5b67.tar.gz
* array.c (flatten): check if reentered. [ruby-dev:34798]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16522 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--array.c8
-rw-r--r--test/ruby/test_array.rb14
3 files changed, 24 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index da20198daf..d6938e99c0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Thu May 22 12:40:54 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * array.c (flatten): check if reentered. [ruby-dev:34798]
+
Thu May 22 11:39:59 2008 Tanaka Akira <akr@fsij.org>
* test/ruby/envutil.rb (assert_normal_exit): capture stdout and stderr
diff --git a/array.c b/array.c
index 3936cede42..2cd3478edb 100644
--- a/array.c
+++ b/array.c
@@ -2831,8 +2831,8 @@ flatten(VALUE ary, int level, int *modified)
st_table *memo;
st_data_t id;
- stack = rb_ary_new();
- result = ary_new(rb_class_of(ary), RARRAY_LEN(ary));
+ stack = ary_new(0, ARY_DEFAULT_SIZE);
+ result = ary_new(0, RARRAY_LEN(ary));
memo = st_init_numtable();
st_insert(memo, (st_data_t)ary, (st_data_t)Qtrue);
*modified = 0;
@@ -2841,6 +2841,9 @@ flatten(VALUE ary, int level, int *modified)
while (i < RARRAY_LEN(ary)) {
elt = RARRAY_PTR(ary)[i++];
tmp = rb_check_array_type(elt);
+ if (RBASIC(result)->klass) {
+ rb_raise(rb_eRuntimeError, "flatten reentered");
+ }
if (NIL_P(tmp) || (level >= 0 && RARRAY_LEN(stack) / 2 >= level)) {
rb_ary_push(result, elt);
}
@@ -2870,6 +2873,7 @@ flatten(VALUE ary, int level, int *modified)
st_free_table(memo);
+ RBASIC(result)->klass = rb_class_of(ary);
return result;
}
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index dd3d165c1f..0cbeb435a9 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -726,6 +726,20 @@ class TestArray < Test::Unit::TestCase
@cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten)
end
+ def test_flatten_with_callcc
+ respond_to?(:callcc) or require 'continuation'
+ o = Object.new
+ def o.to_ary() callcc {|k| @cont = k; [1,2,3]} end
+ begin
+ assert_equal([10, 20, 1, 2, 3, 30, 1, 2, 3, 40], [10, 20, o, 30, o, 40].flatten)
+ rescue => e
+ else
+ o.instance_eval {@cont}.call
+ end
+ assert_instance_of(RuntimeError, e, '[ruby-dev:34798]')
+ assert_match(/reentered/, e.message, '[ruby-dev:34798]')
+ end
+
def test_hash
a1 = @cls[ 'cat', 'dog' ]
a2 = @cls[ 'cat', 'dog' ]