ruby-changes:51345
From: nobu <ko1@a...>
Date: Sat, 2 Jun 2018 11:39:40 +0900 (JST)
Subject: [ruby-changes:51345] nobu:r63551 (trunk): enum.c: mitigate overflows
nobu 2018-06-02 11:39:34 +0900 (Sat, 02 Jun 2018) New Revision: 63551 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63551 Log: enum.c: mitigate overflows * enum.c (enum_count): convert counters to Integer as unsigned long, instead of long, to mitigate overflows. [ruby-core:87348] [Bug #14805] * enum.c (ary_inject_op): ditto. * enum.c (each_with_index_i): ditto, instead of int. * enum.c (find_index_i, find_index_iter_i): ditto, instead of unsigned int. Modified files: trunk/enum.c Index: enum.c =================================================================== --- enum.c (revision 63550) +++ enum.c (revision 63551) @@ -218,7 +218,7 @@ enum_count(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L218 memo = MEMO_NEW(item, 0, 0); rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo); - return LONG2NUM(memo->u3.cnt); + return ULONG2NUM(memo->u3.cnt); } static VALUE @@ -286,7 +286,7 @@ find_index_i(RB_BLOCK_CALL_FUNC_ARGLIST( https://github.com/ruby/ruby/blob/trunk/enum.c#L286 ENUM_WANT_SVALUE(); if (rb_equal(i, memo->v2)) { - MEMO_V1_SET(memo, UINT2NUM(memo->u3.cnt)); + MEMO_V1_SET(memo, ULONG2NUM(memo->u3.cnt)); rb_iter_break(); } memo->u3.cnt++; @@ -299,7 +299,7 @@ find_index_iter_i(RB_BLOCK_CALL_FUNC_ARG https://github.com/ruby/ruby/blob/trunk/enum.c#L299 struct MEMO *memo = MEMO_CAST(memop); if (RTEST(rb_yield_values2(argc, argv))) { - MEMO_V1_SET(memo, UINT2NUM(memo->u3.cnt)); + MEMO_V1_SET(memo, ULONG2NUM(memo->u3.cnt)); rb_iter_break(); } memo->u3.cnt++; @@ -693,7 +693,7 @@ ary_inject_op(VALUE ary, VALUE init, VAL https://github.com/ruby/ruby/blob/trunk/enum.c#L693 if (FIXNUM_P(e)) { n += FIX2LONG(e); /* should not overflow long type */ if (!FIXABLE(n)) { - v = rb_big_plus(LONG2NUM(n), v); + v = rb_big_plus(ULONG2NUM(n), v); n = 0; } } @@ -2234,7 +2234,7 @@ each_with_index_i(RB_BLOCK_CALL_FUNC_ARG https://github.com/ruby/ruby/blob/trunk/enum.c#L2234 { long n = MEMO_CAST(memo)->u3.cnt++; - return rb_yield_values(2, rb_enum_values_pack(argc, argv), INT2NUM(n)); + return rb_yield_values(2, rb_enum_values_pack(argc, argv), ULONG2NUM(n)); } /* -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/