From e16d49fbceb6b4359ca23b58a3c6d7abd4b80690 Mon Sep 17 00:00:00 2001 From: naruse Date: Sat, 24 Feb 2018 07:19:48 +0000 Subject: [PATCH] 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 --- rational.c | 22 ++++++++++++++-------- test/ruby/test_rational.rb | 4 ++++ version.h | 2 +- 3 files changed, 19 insertions(+), 9 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) diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 43658e5454..2152486742 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}/) { diff --git a/version.h b/version.h index 05554ed714..4600b27b85 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.5.0" #define RUBY_RELEASE_DATE "2018-02-24" -#define RUBY_PATCHLEVEL 34 +#define RUBY_PATCHLEVEL 35 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 2