From e1680f8cc1c6ba464710bc164720e47e749b0ac8 Mon Sep 17 00:00:00 2001 From: akr Date: Wed, 12 Jan 2011 10:27:48 +0000 Subject: [PATCH] * enum.c (sort_by_i): reenter check more strictly. (sort_by_cmp): ditto. [ruby-dev:43003] reported by Usaku NAKAMURA. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30521 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ enum.c | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2e2ec0310b..359bdb72c9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Wed Jan 12 19:09:29 2011 Tanaka Akira + + * enum.c (sort_by_i): reenter check more strictly. + (sort_by_cmp): ditto. + [ruby-dev:43003] reported by Usaku NAKAMURA. + Wed Jan 12 16:25:12 2011 Yuki Sonoda (Yugui) * lib/net/http.rb (Net::HTTP#connect): makes it timeout during diff --git a/enum.c b/enum.c index d17d9d497e..823d111bc1 100644 --- a/enum.c +++ b/enum.c @@ -782,14 +782,20 @@ sort_by_i(VALUE i, VALUE _data, int argc, VALUE *argv) { struct sort_by_data *data = (struct sort_by_data *)_data; VALUE ary = data->ary; + VALUE v; ENUM_WANT_SVALUE(); + v = rb_yield(i); + if (RBASIC(ary)->klass) { rb_raise(rb_eRuntimeError, "sort_by reentered"); } - - RARRAY_PTR(data->buf)[data->n*2] = rb_yield(i); + if (RARRAY_LEN(data->buf) != SORT_BY_BUFSIZE*2) { + rb_raise(rb_eRuntimeError, "sort_by reentered"); + } + + RARRAY_PTR(data->buf)[data->n*2] = v; RARRAY_PTR(data->buf)[data->n*2+1] = i; data->n++; if (data->n == SORT_BY_BUFSIZE) { @@ -802,13 +808,17 @@ sort_by_i(VALUE i, VALUE _data, int argc, VALUE *argv) static int sort_by_cmp(const void *ap, const void *bp, void *data) { - VALUE a = *(VALUE *)ap; - VALUE b = *(VALUE *)bp; + VALUE a; + VALUE b; VALUE ary = (VALUE)data; if (RBASIC(ary)->klass) { rb_raise(rb_eRuntimeError, "sort_by reentered"); } + + a = *(VALUE *)ap; + b = *(VALUE *)bp; + return rb_cmpint(rb_funcall(a, id_cmp, 1, b), a, b); }