Skip to content

Validate the elements in Date#marshal_load - #204

Open
mame wants to merge 1 commit into
ruby:masterfrom
mame:date-marshal-load-typecheck
Open

mame wants to merge 1 commit into
ruby:masterfrom
mame:date-marshal-load-typecheck

Conversation

@mame

@mame mame commented Nov 13, 2026

Copy link
Copy Markdown
Member

The six-element form stored nth and sf without checking their types, and accepted values that no Date or DateTime can have. Require an Integer for nth and an Integer or Rational for sf, and reject jd, df, sf, of and sg out of the ranges that a Date or DateTime can have.

Reported-by: Hiroshi SHIBATA hsbt@ruby-lang.org

@mame
mame requested a review from nobu November 13, 2026 02:54
The six-element form stored `nth` and `sf` without checking their
types, and accepted values that no Date or DateTime can have. Require
an Integer for `nth` and an Integer or Rational for `sf`, and reject
`jd`, `df`, `sf`, `of` and `sg` out of the ranges that a Date or
DateTime can have.

Reported-by: Hiroshi SHIBATA <hsbt@ruby-lang.org>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame
mame force-pushed the date-marshal-load-typecheck branch from aca6f17 to b737125 Compare November 13, 2026 04:09
Comment thread ext/date/date_core.c
Comment on lines +7828 to +7842
if (!RB_INTEGER_TYPE_P(nth))
rb_raise(rb_eTypeError, "invalid nth (expected an Integer)");
if (jd < -1 || jd > CM_PERIOD)
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");

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.

Comment thread ext/date/date_core.c

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

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"),

@wtn

wtn commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Some dates no longer survive a Marshal round trip:

d = Date.new(-4713, 12, 31, Date::GREGORIAN)
d.marshal_dump[0, 2]          # => [-1, 213447754]
Marshal.load(Marshal.dump(d)) # => Date::Error: invalid jd

Date.new and Date.jd can store the same day with different jd values:

Date.new(579687, 12, 31, Date::GREGORIAN).marshal_dump[0, 2] # => [0, 213447754]
Date.jd(213447754, Date::GREGORIAN).marshal_dump[0, 2]       # => [1, 37]

Date.jd keeps jd below CM_PERIOD. Date.new computes jd from the year, and for a Gregorian date in the last 38 days of a period the result is above CM_PERIOD, up to CM_PERIOD + 37. A DateTime with a negative offset can be one day higher.

[0, 213447718, 0, 0, 0, 2299161.0] in the new test is the dump of Date.new(579687, 11, 25).

Changing the check to jd > CM_PERIOD + 38 lets these load and still rejects 213447756.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants