Repository navigation
Conversation
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>
aca6f17 to
b737125
Compare
| 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"); |
There was a problem hiding this comment.
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.
|
|
||
| if (!RB_INTEGER_TYPE_P(nth)) | ||
| rb_raise(rb_eTypeError, "invalid nth (expected an Integer)"); | ||
| if (jd < -1 || jd > CM_PERIOD) |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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"), |
There was a problem hiding this comment.
For the negative jd case:
| DateTime.jd(213447716, 23, 59, 59, "-12:00"), | |
| DateTime.jd(213447716, 23, 59, 59, "-12:00"), | |
| DateTime.jd(-213447716, 23, 59, 59, "-12:00"), |
|
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(579687, 12, 31, Date::GREGORIAN).marshal_dump[0, 2] # => [0, 213447754]
Date.jd(213447754, Date::GREGORIAN).marshal_dump[0, 2] # => [1, 37]
Changing the check to |
The six-element form stored
nthandsfwithout checking their types, and accepted values that no Date or DateTime can have. Require an Integer fornthand an Integer or Rational forsf, and rejectjd,df,sf,ofandsgout of the ranges that a Date or DateTime can have.Reported-by: Hiroshi SHIBATA hsbt@ruby-lang.org