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 /ext | |
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
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 6 |
1 files changed, 5 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); |