From b13b3e624b62d321ab5fb84f1aa02e9672a7cc43 Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 13 Jul 2007 17:29:24 +0000 Subject: [PATCH] * numeric.c (int_pow): overflow detection using FIT_SQRT_LONG(). [ruby-dev:31215] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12778 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ numeric.c | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4daf4aeaef..c1601492c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Jul 14 02:27:43 2007 Yukihiro Matsumoto + + * numeric.c (int_pow): overflow detection using FIT_SQRT_LONG(). + [ruby-dev:31215] + Sat Jul 14 02:05:53 2007 Yukihiro Matsumoto * insns.def (opt_div): LONG2FIX() may not work for corner cases, diff --git a/numeric.c b/numeric.c index da8b74bb3b..a50199dce3 100644 --- a/numeric.c +++ b/numeric.c @@ -2046,6 +2046,10 @@ fix_minus(VALUE x, VALUE y) * result. */ +#define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_VALUE*CHAR_BIT-1)/2)) +/*tests if N*N would overflow*/ +#define FIT_SQRT_LONG(n) (((n)=-SQRT_LONG_MAX)) + static VALUE fix_mul(VALUE x, VALUE y) { @@ -2070,9 +2074,6 @@ fix_mul(VALUE x, VALUE y) if (FIXABLE(d)) return LONG2FIX(d); return rb_ll2inum(d); #else -# define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_VALUE*CHAR_BIT-1)/2)) - /*tests if N*N would overflow*/ -# define FIT_SQRT_LONG(n) (((n)=-SQRT_LONG_MAX)) if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b)) return LONG2FIX(a*b); c = a * b; @@ -2288,15 +2289,14 @@ int_pow(long x, unsigned long y) y &= ~1; do { while (y % 2 == 0) { - long x2 = x * x; - if (x2 < x || !POSFIXABLE(x2)) { + if (!FIT_SQRT_LONG(x)) { VALUE v; bignum: v = rb_big_pow(rb_int2big(x), LONG2NUM(y)); if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v); return v; } - x = x2; + x = x * x; y >>= 1; } {