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

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/trunk@62555 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-02-24 02:08:36 +00:00
parent 623160916d
commit f954caea43
2 changed files with 18 additions and 8 deletions

View file

@ -2368,6 +2368,18 @@ islettere(int c)
return (c == 'e' || c == 'E'); 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 static int
read_num(const char **s, const char *const end, VALUE *num, VALUE *div) read_num(const char **s, const char *const end, VALUE *num, VALUE *div)
{ {
@ -2422,7 +2434,7 @@ read_num(const char **s, const char *const end, VALUE *num, VALUE *div)
else { else {
if (fn != ZERO) exp = rb_int_minus(exp, fn); if (fn != ZERO) exp = rb_int_minus(exp, fn);
if (INT_NEGATIVE_P(exp)) { if (INT_NEGATIVE_P(exp)) {
*div = f_expt10(exp); *div = f_expt10(negate_num(exp));
} }
else { else {
*num = rb_int_mul(n, f_expt10(exp)); *num = rb_int_mul(n, f_expt10(exp));
@ -2483,13 +2495,7 @@ parse_rat(const char *s, const char *const e, int strict)
} }
if (sign == '-') { if (sign == '-') {
if (FIXNUM_P(num)) { num = negate_num(num);
num = rb_int_uminus(num);
}
else {
BIGNUM_NEGATE(num);
num = rb_big_norm(num);
}
} }
if (!canonicalization || den != ONE) if (!canonicalization || den != ONE)

View file

@ -110,6 +110,10 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(3),Rational('3')) assert_equal(Rational(3),Rational('3'))
assert_equal(Rational(1),Rational('3.0','3.0')) assert_equal(Rational(1),Rational('3.0','3.0'))
assert_equal(Rational(1),Rational('3/3','3/3')) assert_equal(Rational(1),Rational('3/3','3/3'))
assert_equal(Rational(111, 10), Rational('1.11e+1'))
assert_equal(Rational(111, 10), Rational('1.11e1'))
assert_equal(Rational(111, 100), Rational('1.11e0'))
assert_equal(Rational(111, 1000), Rational('1.11e-1'))
assert_raise(TypeError){Rational(nil)} assert_raise(TypeError){Rational(nil)}
assert_raise(ArgumentError){Rational('')} assert_raise(ArgumentError){Rational('')}
assert_raise_with_message(ArgumentError, /\u{221a 2668}/) { assert_raise_with_message(ArgumentError, /\u{221a 2668}/) {