diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2021-11-09 12:56:45 +0100 |
---|---|---|
committer | Kenta Murata <mrkn@mrkn.jp> | 2021-12-24 02:28:51 +0900 |
commit | ec478d947f218e1b94856941135701fe37e88fbc (patch) | |
tree | 06df0936316a025728ffe2715f4d03e021bf588f | |
parent | c539cfd235b46dfb831fe94b55b547c7ac4a58f6 (diff) | |
download | ruby-ec478d947f218e1b94856941135701fe37e88fbc.tar.gz |
[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
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 6 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal.rb | 10 |
2 files changed, 15 insertions, 1 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index ab3d8d6b81..1d82913229 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 { 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 e860c9d153..8d4d2353e3 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -2089,6 +2089,16 @@ class TestBigDecimal < Test::Unit::TestCase 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"], |