summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authorKazuki Tsujimoto <kazuki@callcc.net>2019-11-19 09:35:47 -0600
committerKazuki Tsujimoto <kazuki@callcc.net>2019-11-19 15:35:14 -0600
commitff41663403d3eb76d95f465cb94e14d2faaa04d1 (patch)
tree28f984be774837a41fd277319a8b2dc12c0501d5 /enum.c
parent61131edba7c885a6d12c4d6f0e502fb40381f184 (diff)
Fix memory corruption in Enumerable#reverse_each [ruby-dev:50867] [Bug #16354]
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/enum.c b/enum.c
index 0653280..a411449 100644
--- a/enum.c
+++ b/enum.c
@@ -2419,14 +2419,20 @@ static VALUE
enum_reverse_each(int argc, VALUE *argv, VALUE obj)
{
VALUE ary;
- long i;
+ long len;
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
ary = enum_to_a(argc, argv, obj);
- for (i = RARRAY_LEN(ary); --i >= 0; ) {
- rb_yield(RARRAY_AREF(ary, i));
+ len = RARRAY_LEN(ary);
+ while (len--) {
+ long nlen;
+ rb_yield(RARRAY_AREF(ary, len));
+ nlen = RARRAY_LEN(ary);
+ if (nlen < len) {
+ len = nlen;
+ }
}
return obj;