summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2024-11-06 13:31:38 +0900
committerNobuyoshi Nakada <[email protected]>2024-11-06 13:31:38 +0900
commitd71be7274bd2623bb521be72c245c08fc38d6ae4 (patch)
tree9b149e6c4bb5f1f974fc129081866f643d0153b1
parent18c3e2d9f1f1d5fc90d95bfa118982aaebc75928 (diff)
[Bug #20873] Consider `-FIXNUM_MIN` overflow
`-FIXNUM_MIN` is usually greater than `FIXNUM_MAX` on platforms using two's complement representation.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12011
-rw-r--r--sprintf.c2
-rw-r--r--test/ruby/test_sprintf.rb4
2 files changed, 5 insertions, 1 deletions
diff --git a/sprintf.c b/sprintf.c
index 1d3b3e0d07..f1ae282123 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -808,7 +808,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
if (FIXNUM_P(num)) {
if ((SIGNED_VALUE)num < 0) {
long n = -FIX2LONG(num);
- num = LONG2FIX(n);
+ num = LONG2NUM(n);
sign = -1;
}
}
diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb
index 498f93aec4..3bd3bc793c 100644
--- a/test/ruby/test_sprintf.rb
+++ b/test/ruby/test_sprintf.rb
@@ -227,6 +227,10 @@ class TestSprintf < Test::Unit::TestCase
bug11766 = '[ruby-core:71806] [Bug #11766]'
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
def test_rational_precision