diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-02-24 07:19:48 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-02-24 07:19:48 +0000 |
commit | e16d49fbceb6b4359ca23b58a3c6d7abd4b80690 (patch) | |
tree | d7692fd642a4699089519fa06880029e17cc36b1 /rational.c | |
parent | a94362d31f4dffd83494afc4909e8f5e311571bd (diff) |
merge revision(s) 62555: [Backport #14547]
rational.c: segfault on Rational exponent
* rational.c (read_num): fix segfault on Rational() with positive
but less than the length of fractional part exponent. should be
negated to convert to divisor which is a reciprocal.
[ruby-core:85783] [Bug #14547]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_5@62562 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'rational.c')
-rw-r--r-- | rational.c | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/rational.c b/rational.c index d1453aa9f7..a29aec4000 100644 --- a/rational.c +++ b/rational.c @@ -2366,6 +2366,18 @@ islettere(int c) return (c == 'e' || c == 'E'); } +static VALUE +negate_num(VALUE num) +{ + if (FIXNUM_P(num)) { + return rb_int_uminus(num); + } + else { + BIGNUM_NEGATE(num); + return rb_big_norm(num); + } +} + static int read_num(const char **s, const char *const end, VALUE *num, VALUE *div) { @@ -2420,7 +2432,7 @@ read_num(const char **s, const char *const end, VALUE *num, VALUE *div) else { if (fn != ZERO) exp = rb_int_minus(exp, fn); if (INT_NEGATIVE_P(exp)) { - *div = f_expt10(exp); + *div = f_expt10(negate_num(exp)); } else { *num = rb_int_mul(n, f_expt10(exp)); @@ -2481,13 +2493,7 @@ parse_rat(const char *s, const char *const e, int strict) } if (sign == '-') { - if (FIXNUM_P(num)) { - num = rb_int_uminus(num); - } - else { - BIGNUM_NEGATE(num); - num = rb_big_norm(num); - } + num = negate_num(num); } if (!canonicalization || den != ONE) |