ruby-changes:65359
From: usa <ko1@a...>
Date: Sun, 28 Feb 2021 22:47:47 +0900 (JST)
Subject: [ruby-changes:65359] b64876ae55 (ruby_2_6): merge revision(s) f364564e66d1db1de8e80d669287386595c8bc46: [Backport #16269]
https://git.ruby-lang.org/ruby.git/commit/?id=b64876ae55 From b64876ae559b05949c6857fb3d76822c2f7c4df2 Mon Sep 17 00:00:00 2001 From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Sun, 28 Feb 2021 13:47:29 +0000 Subject: merge revision(s) f364564e66d1db1de8e80d669287386595c8bc46: [Backport #16269] bignum.c (estimate_initial_sqrt): prevent integer overflow `Integer.sqrt(0xffff_ffff_ffff_ffff ** 2)` caused assertion failure because of integer overflow. [ruby-core:95453] [Bug #16269] --- bignum.c | 10 +++++++++- test/ruby/test_integer.rb | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67894 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- bignum.c | 10 +++++++++- test/ruby/test_integer.rb | 3 +++ version.h | 8 ++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/bignum.c b/bignum.c index ee3b49f..302774b 100644 --- a/bignum.c +++ b/bignum.c @@ -6875,7 +6875,15 @@ estimate_initial_sqrt(VALUE *xp, const size_t xn, const BDIGIT *nds, size_t len) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6875 rshift /= 2; rshift += (2-(len&1))*BITSPERDIG/2; if (rshift >= 0) { - d <<= rshift; + if (nlz((BDIGIT)d) + rshift >= BITSPERDIG) { + /* (d << rshift) does cause overflow. + * example: Integer.sqrt(0xffff_ffff_ffff_ffff ** 2) + */ + d = ~(BDIGIT_DBL)0; + } + else { + d <<= rshift; + } } BDIGITS_ZERO(xds, xn-2); bdigitdbl2bary(&xds[xn-2], 2, d); diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 69347b6..0f289d8 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -595,6 +595,9 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L595 failures << n unless root*root <= n && (root+1)*(root+1) > n end assert_empty(failures, bug13440) + + x = 0xffff_ffff_ffff_ffff + assert_equal(x, Integer.sqrt(x ** 2), "[ruby-core:95453]") end def test_fdiv diff --git a/version.h b/version.h index 373ee70..408caf4 100644 --- a/version.h +++ b/version.h @@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L1 #define RUBY_VERSION "2.6.7" -#define RUBY_RELEASE_DATE "2021-01-31" -#define RUBY_PATCHLEVEL 153 +#define RUBY_RELEASE_DATE "2021-02-28" +#define RUBY_PATCHLEVEL 154 #define RUBY_RELEASE_YEAR 2021 -#define RUBY_RELEASE_MONTH 1 -#define RUBY_RELEASE_DAY 31 +#define RUBY_RELEASE_MONTH 2 +#define RUBY_RELEASE_DAY 28 #include "ruby/version.h" -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/