diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-02-24 02:08:36 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-02-24 02:08:36 +0000 |
commit | 151fdd50fe1dcf7aadd170bad03a93e59015bc73 (patch) | |
tree | e798916262b7a73f6b716df8d4ed7aa9929d2e65 | |
parent | 201380b5c4b22b08604acfbce2e51efdf4f87bc6 (diff) | |
download | ruby-151fdd50fe1dcf7aadd170bad03a93e59015bc73.tar.gz |
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
-rw-r--r-- | rational.c | 22 | ||||
-rw-r--r-- | test/ruby/test_rational.rb | 4 |
2 files changed, 18 insertions, 8 deletions
diff --git a/rational.c b/rational.c index 3fbfc7f1b1..b57677031d 100644 --- a/rational.c +++ b/rational.c @@ -2368,6 +2368,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) { @@ -2422,7 +2434,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)); @@ -2483,13 +2495,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) diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 74fbbb49ec..fe267d5a1f 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -110,6 +110,10 @@ class Rational_Test < Test::Unit::TestCase assert_equal(Rational(3),Rational('3')) assert_equal(Rational(1),Rational('3.0','3.0')) 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(ArgumentError){Rational('')} assert_raise_with_message(ArgumentError, /\u{221a 2668}/) { |