Skip to content

Commit 022c18b

Browse files
Stranger6667matzbot
authored andcommitted
[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> ruby/date@67d75e8423
1 parent c1877d4 commit 022c18b

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

ext/date/date_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ m_ajd(union DateData *x)
15991599

16001600
if (simple_dat_p(x)) {
16011601
r = m_real_jd(x);
1602-
if (FIXNUM_P(r) && FIX2LONG(r) <= (FIXNUM_MAX / 2)) {
1602+
if (FIXNUM_P(r) && FIX2LONG(r) <= (FIXNUM_MAX / 2) && FIX2LONG(r) >= (FIXNUM_MIN + 1) / 2) {
16031603
long ir = FIX2LONG(r);
16041604
ir = ir * 2 - 1;
16051605
return rb_rational_new2(LONG2FIX(ir), INT2FIX(2));

test/date/test_switch_hitter.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ def test_jd
9797
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.offset])
9898
end
9999

100+
def test_ajd
101+
assert_equal(Date.civil(2008, 1, 16).ajd, 4908963r/2)
102+
assert_equal(Date.civil(-11082381539297990, 2, 19).ajd, -8095679714453739481r/2)
103+
end
104+
100105
def test_ordinal
101106
d = Date.ordinal
102107
assert_equal([-4712, 1], [d.year, d.yday])

0 commit comments

Comments
 (0)