From 6bb70c11b344dee0739c6fb7bbf0ea124b5225b6 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 2 Jun 2018 02:39:34 +0000 Subject: 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63551 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- enum.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'enum.c') diff --git a/enum.c b/enum.c index c482e7b8ce..967bc6bada 100644 --- a/enum.c +++ b/enum.c @@ -218,7 +218,7 @@ enum_count(int argc, VALUE *argv, VALUE obj) 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(i, memop)) 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_ARGLIST(i, memop)) 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, VALUE op) 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_ARGLIST(i, memo)) { 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)); } /* -- cgit v1.2.3