From 20eebb8542143ffbccbace932ca0277ee16cf6f0 Mon Sep 17 00:00:00 2001 From: naruse Date: Wed, 19 May 2010 09:22:59 +0000 Subject: [PATCH] * numeric.c (rb_num2ulong): use rb_big2ulong for data from Bignum. Without this 32bit integer on 32bit environment can't converted into long. This fixes 1) and 2) of [ruby-dev:41289] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ numeric.c | 31 ++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index d92ba09d67..847c595111 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Wed May 19 16:55:09 2010 NARUSE, Yui + + * numeric.c (rb_num2ulong): use rb_big2ulong for data from + Bignum. Without this 32bit integer on 32bit environment + can't converted into long. + This fixes 1) and 2) of [ruby-dev:41289] + Mon May 17 22:19:16 2010 Yusuke Endoh * process.c: suppress warning for signed and unsigned type diff --git a/numeric.c b/numeric.c index a9fa089ccc..fec4794330 100644 --- a/numeric.c +++ b/numeric.c @@ -1722,10 +1722,35 @@ rb_num2long(VALUE val) VALUE rb_num2ulong(VALUE val) { - if (TYPE(val) == T_BIGNUM) { - return rb_big2ulong(val); + again: + if (NIL_P(val)) { + rb_raise(rb_eTypeError, "no implicit conversion from nil to integer"); + } + + if (FIXNUM_P(val)) return FIX2LONG(val); /* this is FIX2LONG, inteneded */ + + switch (TYPE(val)) { + case T_FLOAT: + if (RFLOAT_VALUE(val) <= (double)LONG_MAX + && RFLOAT_VALUE(val) >= (double)LONG_MIN) { + return (RFLOAT_VALUE(val)); + } + else { + char buf[24]; + char *s; + + snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); + if ((s = strchr(buf, ' ')) != 0) *s = '\0'; + rb_raise(rb_eRangeError, "float %s out of range of integer", buf); + } + + case T_BIGNUM: + return rb_big2ulong(val); + + default: + val = rb_to_int(val); + goto again; } - return (VALUE)rb_num2long(val); } #if SIZEOF_INT < SIZEOF_VALUE