[前][次][番号順一覧][スレッド一覧]

ruby-changes:70465

From: Jean <ko1@a...>
Date: Fri, 24 Dec 2021 02:29:44 +0900 (JST)
Subject: [ruby-changes:70465] ec478d947f (master): [ruby/bigdecimal] Fix negative Bignum conversion

https://git.ruby-lang.org/ruby.git/commit/?id=ec478d947f

From ec478d947f218e1b94856941135701fe37e88fbc Mon Sep 17 00:00:00 2001
From: Jean Boussier <jean.boussier@g...>
Date: Tue, 9 Nov 2021 12:56:45 +0100
Subject: [ruby/bigdecimal] Fix negative Bignum conversion

Introduced in https://github.com/ruby/bigdecimal/commit/4792a917d806

`rb_absint_size` return the number of bytes needed to fit
the absolute integer, but negative integers need the sign, so one more
bit, and potentially one more byte.

https://github.com/ruby/bigdecimal/commit/0f3d5d0eb7
---
 ext/bigdecimal/bigdecimal.c        |  6 +++++-
 test/bigdecimal/test_bigdecimal.rb | 10 ++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index ab3d8d6b81d..1d829132290 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -2770,8 +2770,12 @@ rb_big_convert_to_BigDecimal(VALUE val, RB_UNUSED_VAR(size_t digs), int raise_ex https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2770
 {
     assert(RB_TYPE_P(val, T_BIGNUM));
 
-    size_t size = rb_absint_size(val, NULL);
+    int leading_zeros;
+    size_t size = rb_absint_size(val, &leading_zeros);
     int sign = FIX2INT(rb_big_cmp(val, INT2FIX(0)));
+    if (sign < 0 && leading_zeros == 0) {
+        size += 1;
+    }
     if (size <= sizeof(long)) {
         if (sign < 0) {
             return rb_int64_convert_to_BigDecimal(NUM2LONG(val), digs, raise_exception);
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index e860c9d1534..8d4d2353e32 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -2089,6 +2089,16 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L2089
     assert_raise(err) { bd.send(:initialize_dup, bd2) }
   end
 
+  def test_llong_min
+    # https://github.com/ruby/bigdecimal/issues/199
+    # Between LLONG_MIN and -ULLONG_MAX
+    llong_min = -(2 ** 63 + 1)
+    assert_equal BigDecimal(llong_min.to_s), BigDecimal(llong_min)
+
+    minus_ullong_max = -(2 ** 64 - 1)
+    assert_equal BigDecimal(minus_ullong_max.to_s), BigDecimal(minus_ullong_max)
+  end
+
   def assert_no_memory_leak(code, *rest, **opt)
     code = "8.times {20_000.times {begin #{code}; rescue NoMemoryError; end}; GC.start}"
     super(["-rbigdecimal"],
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]