From ff910d8494149eb2adc37e5bd7de24bd4858752d Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Wed, 4 Nov 2026 17:10:25 +0000 Subject: [PATCH 1/2] Return nil from Date#<=> for a NaN Date#<=> passed the result of ajd <=> other to rb_cmpint, which turns the nil that Rational#<=> returns for a NaN into an ArgumentError, so Date <=> Float::NAN raised and Date == Float::NAN raised with it. Return nil when ajd <=> other does, as Integer#<=> and Rational#<=> do, and keep normalizing other results to -1, 0 or 1. --- ext/date/date_core.c | 6 ++++-- test/date/test_date_arith.rb | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index b3040171..5603a1da 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -6937,8 +6937,10 @@ cmp_gen(VALUE self, VALUE other) { get_d1(self); - if (k_numeric_p(other)) - return INT2FIX(f_cmp(m_ajd(dat), other)); + if (k_numeric_p(other)) { + VALUE c = rb_funcallv(m_ajd(dat), id_cmp, 1, &other); + return NIL_P(c) ? Qnil : INT2FIX(rb_cmpint(c, self, other)); + } else if (k_date_p(other)) return INT2FIX(f_cmp(m_ajd(dat), f_ajd(other))); return rb_num_coerce_cmp(self, other, id_cmp); diff --git a/test/date/test_date_arith.rb b/test/date/test_date_arith.rb index e148dabc..58a5d6d5 100644 --- a/test/date/test_date_arith.rb +++ b/test/date/test_date_arith.rb @@ -147,6 +147,15 @@ def test__compare DateTime.new(2001,2,3,0,0,0,'+00:00'))) assert_equal(-1, (DateTime.new(2001,2,3,0,0,0,'+09:00') <=> DateTime.new(2001,2,2,16,0,0,'+00:00'))) + n = Class.new(Numeric) {def coerce(x) [self, x] end; def <=>(x) 5 end}.new + assert_equal(1, (Date.new(2000,1,1) <=> n)) + end + + def test__compare_nan + [Date.new(2000,1,1), DateTime.new(2000,1,1,12)].each do |d| + assert_nil(d <=> Float::NAN) + assert_equal(false, d == Float::NAN) + end end def test_prev From 1c273d40ccfea71f8a9db0fc3fe9b4111cd8fb73 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Sat, 14 Nov 2026 11:01:55 +0000 Subject: [PATCH 2/2] Raise ArgumentError from Date#step for an incomparable limit Date#step, #upto and #downto passed the result of Date#<=> to FIX2INT, which expects a Fixnum; for nil it raises TypeError on 64-bit Linux and macOS and its behaviour depends on the build elsewhere. Use rb_cmpint, which raises ArgumentError for nil. --- ext/date/date_core.c | 8 ++++---- test/date/test_date_arith.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 5603a1da..9e9337da 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -6872,7 +6872,7 @@ d_lite_step(int argc, VALUE *argv, VALUE self) date = self; c = f_cmp(step, INT2FIX(0)); if (c < 0) { - while (FIX2INT(d_lite_cmp(date, limit)) >= 0) { + while (rb_cmpint(d_lite_cmp(date, limit), date, limit) >= 0) { rb_yield(date); date = d_lite_plus(date, step); } @@ -6882,7 +6882,7 @@ d_lite_step(int argc, VALUE *argv, VALUE self) rb_yield(date); } else /* if (c > 0) */ { - while (FIX2INT(d_lite_cmp(date, limit)) <= 0) { + while (rb_cmpint(d_lite_cmp(date, limit), date, limit) <= 0) { rb_yield(date); date = d_lite_plus(date, step); } @@ -6904,7 +6904,7 @@ d_lite_upto(VALUE self, VALUE max) RETURN_ENUMERATOR(self, 1, &max); date = self; - while (FIX2INT(d_lite_cmp(date, max)) <= 0) { + while (rb_cmpint(d_lite_cmp(date, max), date, max) <= 0) { rb_yield(date); date = d_lite_plus(date, INT2FIX(1)); } @@ -6925,7 +6925,7 @@ d_lite_downto(VALUE self, VALUE min) RETURN_ENUMERATOR(self, 1, &min); date = self; - while (FIX2INT(d_lite_cmp(date, min)) >= 0) { + while (rb_cmpint(d_lite_cmp(date, min), date, min) >= 0) { rb_yield(date); date = d_lite_plus(date, INT2FIX(-1)); } diff --git a/test/date/test_date_arith.rb b/test/date/test_date_arith.rb index 58a5d6d5..bdab5399 100644 --- a/test/date/test_date_arith.rb +++ b/test/date/test_date_arith.rb @@ -380,4 +380,15 @@ def o.<=>(*);2;end p.step(q, o) {|d| a << d} assert_empty(a) end + + def test_step__incomparable_limit + d = Date.new(2000,1,1) + [Float::NAN, Object.new].each do |limit| + no = proc {flunk("yielded for #{limit.inspect}")} + assert_raise(ArgumentError) { d.upto(limit, &no) } + assert_raise(ArgumentError) { d.downto(limit, &no) } + assert_raise(ArgumentError) { d.step(limit, 1, &no) } + assert_raise(ArgumentError) { d.step(limit, -1, &no) } + end + end end