1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* bignum.c (rb_ll2big): Don't overflow on signed integer negation.

* ext/bigdecimal/bigdecimal.c (MUL_OVERFLOW_SIGNED_VALUE_P): New
  macro.
  (AddExponent): Don't overflow on signed integer multiplication.
  (VpCtoV): Don't overflow on signed integer arithmetic.
  (VpCtoV): Don't overflow on signed integer arithmetic.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40214 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-04-10 03:34:38 +00:00
parent ea16ec561f
commit fa9b5bf223
3 changed files with 40 additions and 2 deletions

View file

@ -1,3 +1,13 @@
Wed Apr 10 12:32:37 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (rb_ll2big): Don't overflow on signed integer negation.
* ext/bigdecimal/bigdecimal.c (MUL_OVERFLOW_SIGNED_VALUE_P): New
macro.
(AddExponent): Don't overflow on signed integer multiplication.
(VpCtoV): Don't overflow on signed integer arithmetic.
(VpCtoV): Don't overflow on signed integer arithmetic.
Wed Apr 10 06:32:12 2013 Tanaka Akira <akr@fsij.org>
* internal.h (MUL_OVERFLOW_INT_P): New macro.

View file

@ -832,13 +832,17 @@ static VALUE
rb_ll2big(LONG_LONG n)
{
long neg = 0;
unsigned LONG_LONG u;
VALUE big;
if (n < 0) {
n = -n;
u = 1 + (unsigned LONG_LONG)(-(n + 1)); /* u = -n avoiding overflow */
neg = 1;
}
big = rb_ull2big(n);
else {
u = n;
}
big = rb_ull2big(u);
if (neg) {
RBIGNUM_SET_SIGN(big, 0);
}

View file

@ -38,6 +38,16 @@
/* #define ENABLE_NUMERIC_STRING */
#define MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
(a) == 0 ? 0 : \
(a) == -1 ? (b) < -(max) : \
(a) > 0 ? \
((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \
((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b)))
#define SIGNED_VALUE_MAX INTPTR_MAX
#define SIGNED_VALUE_MIN INTPTR_MIN
#define MUL_OVERFLOW_SIGNED_VALUE_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, SIGNED_VALUE_MIN, SIGNED_VALUE_MAX)
VALUE rb_cBigDecimal;
VALUE rb_mBigMath;
@ -3735,12 +3745,18 @@ AddExponent(Real *a, SIGNED_VALUE n)
SIGNED_VALUE eb, mb;
if (e > 0) {
if (n > 0) {
if (MUL_OVERFLOW_SIGNED_VALUE_P(m, (SIGNED_VALUE)BASE_FIG) ||
MUL_OVERFLOW_SIGNED_VALUE_P(e, (SIGNED_VALUE)BASE_FIG))
goto overflow;
mb = m*(SIGNED_VALUE)BASE_FIG;
eb = e*(SIGNED_VALUE)BASE_FIG;
if (mb < eb) goto overflow;
}
}
else if (n < 0) {
if (MUL_OVERFLOW_SIGNED_VALUE_P(m, (SIGNED_VALUE)BASE_FIG) ||
MUL_OVERFLOW_SIGNED_VALUE_P(e, (SIGNED_VALUE)BASE_FIG))
goto underflow;
mb = m*(SIGNED_VALUE)BASE_FIG;
eb = e*(SIGNED_VALUE)BASE_FIG;
if (mb > eb) goto underflow;
@ -5254,9 +5270,17 @@ VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, con
++me;
}
while (i < me) {
if (MUL_OVERFLOW_SIGNED_VALUE_P(e, (SIGNED_VALUE)BASE_FIG))
goto exp_overflow;
es = e * (SIGNED_VALUE)BASE_FIG;
if (MUL_OVERFLOW_SIGNED_VALUE_P(e, 10) ||
SIGNED_VALUE_MAX - (exp_chr[i] - '0') < e * 10)
goto exp_overflow;
e = e * 10 + exp_chr[i] - '0';
if (MUL_OVERFLOW_SIGNED_VALUE_P(e, (SIGNED_VALUE)BASE_FIG))
goto exp_overflow;
if (es > (SIGNED_VALUE)(e * BASE_FIG)) {
exp_overflow:
exponent_overflow = 1;
e = es; /* keep sign */
break;