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 |
Date.new and DateTime.new can store a jd above CM_PERIOD for the last days of a Gregorian period, so such dates failed to round-trip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thank you all. I haven't been able to follow every detail, but for now I changed the check to |
|
Thank you for the update. The crash is gone with this branch, and round trips work with the new Two small points about the new tests:
One more thing from AI agent review: the 2-element and 3-element forms accept Details(Edited: the offset part was already fixed on master by #205, so only the Float case is left here.) When the first element of the old forms is a Float, y = DateTime.allocate.marshal_load([2451545.25, 0, 2299161.0])
y.marshal_dump[3] # => 0.0
Marshal.load(Marshal.dump(y)) # => TypeError: invalid second fraction (expected an Integer or Rational)The round trip succeeds on master. The old forms do not seem to crash. I think adding the same Integer or Rational check for if (!RB_INTEGER_TYPE_P(*rsf) && !RB_TYPE_P(*rsf, T_RATIONAL))
rb_raise(rb_eTypeError, "invalid second fraction (expected an Integer or Rational)");With this change the example raises TypeError at assert_raise(TypeError) { DateTime.allocate.marshal_load([2451545.25, 0, 2299161.0]) } |
The df and sf tests used the invalid start 0.0, so they passed without the range checks. The "+24:00" offset was not accepted and became 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thank you! I think I fixed both points about the tests. As for the 2-element and 3-element forms, I'd appreciate it if you could send a separate PR. |
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