[ruby/date] [Bug #21436] check for fixnum lower bound in m_ajd

Issue - https://bugs.ruby-lang.org/issues/21436

Apparently, the lower bound check is missing, which results in overflow & wrapping later on in RB_INT2FIX

Signed-off-by: Dmitry Dygalo <dmitry.dygalo@workato.com>

67d75e8423
This commit is contained in:
Dmitry Dygalo 2025-06-15 09:12:41 -07:00 committed by git
parent c1877d431e
commit 022c18b60d
2 changed files with 6 additions and 1 deletions

View file

@ -1599,7 +1599,7 @@ m_ajd(union DateData *x)
if (simple_dat_p(x)) { if (simple_dat_p(x)) {
r = m_real_jd(x); r = m_real_jd(x);
if (FIXNUM_P(r) && FIX2LONG(r) <= (FIXNUM_MAX / 2)) { if (FIXNUM_P(r) && FIX2LONG(r) <= (FIXNUM_MAX / 2) && FIX2LONG(r) >= (FIXNUM_MIN + 1) / 2) {
long ir = FIX2LONG(r); long ir = FIX2LONG(r);
ir = ir * 2 - 1; ir = ir * 2 - 1;
return rb_rational_new2(LONG2FIX(ir), INT2FIX(2)); return rb_rational_new2(LONG2FIX(ir), INT2FIX(2));

View file

@ -97,6 +97,11 @@ class TestSH < Test::Unit::TestCase
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset]) [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
end end
def test_ajd
assert_equal(Date.civil(2008, 1, 16).ajd, 4908963r/2)
assert_equal(Date.civil(-11082381539297990, 2, 19).ajd, -8095679714453739481r/2)
end
def test_ordinal def test_ordinal
d = Date.ordinal d = Date.ordinal
assert_equal([-4712, 1], [d.year, d.yday]) assert_equal([-4712, 1], [d.year, d.yday])