[Bug #20873] Consider -FIXNUM_MIN overflow

`-FIXNUM_MIN` is usually greater than `FIXNUM_MAX` on platforms using
two's complement representation.
This commit is contained in:
Nobuyoshi Nakada 2024-11-06 13:31:38 +09:00
parent 18c3e2d9f1
commit d71be7274b
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2024-11-06 05:03:17 +00:00
2 changed files with 5 additions and 1 deletions

View File

@ -808,7 +808,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
if (FIXNUM_P(num)) { if (FIXNUM_P(num)) {
if ((SIGNED_VALUE)num < 0) { if ((SIGNED_VALUE)num < 0) {
long n = -FIX2LONG(num); long n = -FIX2LONG(num);
num = LONG2FIX(n); num = LONG2NUM(n);
sign = -1; sign = -1;
} }
} }

View File

@ -227,6 +227,10 @@ class TestSprintf < Test::Unit::TestCase
bug11766 = '[ruby-core:71806] [Bug #11766]' bug11766 = '[ruby-core:71806] [Bug #11766]'
assert_equal("x"*10+" 1.0", sprintf("x"*10+"%8.1f", 1r), bug11766) assert_equal("x"*10+" 1.0", sprintf("x"*10+"%8.1f", 1r), bug11766)
require 'rbconfig/sizeof'
fmin, fmax = RbConfig::LIMITS.values_at("FIXNUM_MIN", "FIXNUM_MAX")
assert_match(/\A-\d+\.\d+\z/, sprintf("%f", Rational(fmin, fmax)))
end end
def test_rational_precision def test_rational_precision