Repository navigation
Validate the elements in Date#marshal_load #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GitHub shows tabs wrongly. |
||
| } | ||
| break; | ||
| default: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -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"), | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the negative jd case:
Suggested change
|
||||||||
| 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 | ||||||||
There was a problem hiding this comment.
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)?There was a problem hiding this comment.
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: