Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ext/date/date_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7824,6 +7824,22 @@ d_lite_marshal_load(VALUE self, VALUE a)
sf = RARRAY_AREF(a, 3);
of = NUM2INT(RARRAY_AREF(a, 4));
sg = NUM2DBL(RARRAY_AREF(a, 5));

if (!RB_INTEGER_TYPE_P(nth))
rb_raise(rb_eTypeError, "invalid nth (expected an Integer)");
if (jd < -1 || jd > CM_PERIOD)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date should support negative jd. Does Marshal.load(Marshal.dump(Date.jd(-2))) still work with this (maybe there is a different marshal encoding for this, I'm not sure)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Date.jd(-2) still round-trips:

d = Date.jd(-2)
d.marshal_dump[0, 2]               # => [-1, 213447715]
Marshal.load(Marshal.dump(d)) == d # => true

rb_raise(eDateError, "invalid jd");
if (df < 0 || df >= DAY_IN_SECONDS)
rb_raise(eDateError, "invalid day fraction");
if (!RB_INTEGER_TYPE_P(sf) && !RB_TYPE_P(sf, T_RATIONAL))
rb_raise(rb_eTypeError, "invalid second fraction (expected an Integer or Rational)");
if (f_lt_p(sf, INT2FIX(0)) ||
f_ge_p(sf, INT2FIX(SECOND_IN_NANOSECONDS)))
rb_raise(eDateError, "invalid second fraction");
if (of < -DAY_IN_SECONDS || of > DAY_IN_SECONDS)
rb_raise(eDateError, "invalid offset");
if (!c_valid_start_p(sg))
rb_raise(eDateError, "invalid start");
Comment on lines +7828 to +7842

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation here looks weird. If it's consistent with the rest of the file, I guess it is fine, but otherwise I recommend indenting the if body further than the if clause.

@nobu nobu Nov 17, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub shows tabs wrongly.

}
break;
default:
Expand Down
38 changes: 38 additions & 0 deletions test/date/test_date_marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,42 @@ def test_memsize
assert_equal(t, t2)
assert_equal(size, ObjectSpace.memsize_of(t2), "not reallocated but memsize changed")
end

def test_marshal_load_invalid_types
d = Date.allocate
assert_raise(TypeError) { d.marshal_load([1.5, 0, 0, 0, 0, 0.0]) }
assert_raise(TypeError) { d.marshal_load([Rational(1, 2), 0, 0, 0, 0, 0.0]) }
assert_raise(TypeError) { d.marshal_load(["1", 0, 0, 0, 0, 0.0]) }
assert_raise(TypeError) { d.marshal_load([nil, 0, 0, 0, 0, 0.0]) }
assert_raise(TypeError) { d.marshal_load([0, 0, 0, 1.5, 0, 0.0]) }
assert_raise(TypeError) { d.marshal_load([0, 0, 0, "0", 0, 0.0]) }
assert_raise(TypeError) { d.marshal_load([0, 0, 0, Date.new(2000), 0, 0.0]) }
assert_raise(Date::Error) { d.marshal_load([0, 0, 86400, 0, 0, 0.0]) }
assert_raise(Date::Error) { d.marshal_load([0, 0, 0, -1, 0, 0.0]) }
assert_raise(Date::Error) { d.marshal_load([0, 0, 0, 1_000_000_000, 0, 0.0]) }
assert_raise(Date::Error) { d.marshal_load([0, -2, 0, 0, 0, 2299161.0]) }
assert_raise(Date::Error) { d.marshal_load([0, 213447718, 0, 0, 0, 2299161.0]) }
assert_raise(Date::Error) { d.marshal_load([0, 0, 0, 0, 86401, 2299161.0]) }
assert_raise(Date::Error) { d.marshal_load([0, 0, 0, 0, 0, 0.0]) }
assert_raise(Date::Error) { d.marshal_load([0, 0, 0, 0, 0, Float::NAN]) }
assert_equal(Date.new(2000, 1, 1), d.marshal_load([0, 2451545, 0, 0, 0, 2299161.0]))
dt = DateTime.allocate.marshal_load([0, 2451545, 0, Rational(1_000_000_000, 3), 0, 2299161.0])
assert_equal(Rational(1, 3), dt.sec_fraction)
assert_equal(Date.new(10**20), Date.allocate.marshal_load(Date.new(10**20).marshal_dump))
[
DateTime.new(-4712, 1, 1, 0, 0, 0, "+12:00"),
DateTime.jd(213447716, 23, 59, 59, "-12:00"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the negative jd case:

Suggested change
DateTime.jd(213447716, 23, 59, 59, "-12:00"),
DateTime.jd(213447716, 23, 59, 59, "-12:00"),
DateTime.jd(-213447716, 23, 59, 59, "-12:00"),

DateTime.new(2001, 2, 3, 4, 5, 6, "+24:00"),
Date.new(2001, 2, 3, Date::GREGORIAN),
Date.new(2001, 2, 3, Date::JULIAN),
].each do |x|
assert_equal(x, x.class.allocate.marshal_load(x.marshal_dump))
end
end

def test_marshal_load_yaml_marshalable
require "yaml"
doc = "--- !ruby/marshalable:Date\n0: 0\n0: !ruby/marshalable:Date {1.5: 0, 0: 0, 0: 0.0}\n0: 0.0\n"
assert_raise(TypeError) { YAML.safe_load(doc, permitted_classes: [Date]) }
end
end
Loading