From b73712581bb2d2dbae198c4e0fa5da3673259a66 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Wed, 28 Oct 2026 22:58:22 +0900 Subject: [PATCH 1/3] Validate the elements in Date#marshal_load 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 Co-Authored-By: Claude Opus 5.5 --- ext/date/date_core.c | 16 ++++++++++++++ test/date/test_date_marshal.rb | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 575e0e49..60897006 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -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"); } break; default: diff --git a/test/date/test_date_marshal.rb b/test/date/test_date_marshal.rb index fa8b2d91..d54c42f6 100644 --- a/test/date/test_date_marshal.rb +++ b/test/date/test_date_marshal.rb @@ -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"), + 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 From 9c98222c17816aefb6ef9db14c216babc37e98b4 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Wed, 25 Nov 2026 18:08:36 +0900 Subject: [PATCH 2/3] Accept jd up to CM_PERIOD + 38 in Date#marshal_load 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 --- ext/date/date_core.c | 2 +- test/date/test_date_marshal.rb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 60897006..1b3206be 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -7827,7 +7827,7 @@ d_lite_marshal_load(VALUE self, VALUE a) if (!RB_INTEGER_TYPE_P(nth)) rb_raise(rb_eTypeError, "invalid nth (expected an Integer)"); - if (jd < -1 || jd > CM_PERIOD) + if (jd < -1 || jd > CM_PERIOD + 38) rb_raise(eDateError, "invalid jd"); if (df < 0 || df >= DAY_IN_SECONDS) rb_raise(eDateError, "invalid day fraction"); diff --git a/test/date/test_date_marshal.rb b/test/date/test_date_marshal.rb index d54c42f6..4d7aefa7 100644 --- a/test/date/test_date_marshal.rb +++ b/test/date/test_date_marshal.rb @@ -70,7 +70,7 @@ def test_marshal_load_invalid_types 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, 213447756, 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]) } @@ -81,6 +81,11 @@ def test_marshal_load_invalid_types [ DateTime.new(-4712, 1, 1, 0, 0, 0, "+12:00"), DateTime.jd(213447716, 23, 59, 59, "-12:00"), + DateTime.jd(-213447716, 23, 59, 59, "-12:00"), + DateTime.new(-4712, 1, 1, 0, 0, 0, 1), + DateTime.new(-4713, 12, 31, 23, 59, 59, -1, Date::GREGORIAN), + Date.new(-4713, 12, 31, Date::GREGORIAN), + Date.new(579687, 12, 31, Date::GREGORIAN), DateTime.new(2001, 2, 3, 4, 5, 6, "+24:00"), Date.new(2001, 2, 3, Date::GREGORIAN), Date.new(2001, 2, 3, Date::JULIAN), From 1c2e9c24cb0872715c410f3fd26261fec1ce0883 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Wed, 25 Nov 2026 22:20:46 +0900 Subject: [PATCH 3/3] Use a valid start in the Date#marshal_load range tests 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 --- test/date/test_date_marshal.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/date/test_date_marshal.rb b/test/date/test_date_marshal.rb index 4d7aefa7..d99b0c82 100644 --- a/test/date/test_date_marshal.rb +++ b/test/date/test_date_marshal.rb @@ -66,9 +66,9 @@ def test_marshal_load_invalid_types 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, 0, 86400, 0, 0, 2299161.0]) } + assert_raise(Date::Error) { d.marshal_load([0, 0, 0, -1, 0, 2299161.0]) } + assert_raise(Date::Error) { d.marshal_load([0, 0, 0, 1_000_000_000, 0, 2299161.0]) } assert_raise(Date::Error) { d.marshal_load([0, -2, 0, 0, 0, 2299161.0]) } assert_raise(Date::Error) { d.marshal_load([0, 213447756, 0, 0, 0, 2299161.0]) } assert_raise(Date::Error) { d.marshal_load([0, 0, 0, 0, 86401, 2299161.0]) } @@ -86,7 +86,7 @@ def test_marshal_load_invalid_types DateTime.new(-4713, 12, 31, 23, 59, 59, -1, Date::GREGORIAN), Date.new(-4713, 12, 31, Date::GREGORIAN), Date.new(579687, 12, 31, Date::GREGORIAN), - DateTime.new(2001, 2, 3, 4, 5, 6, "+24:00"), + DateTime.new(2001, 2, 3, 4, 5, 6, Rational(1)), Date.new(2001, 2, 3, Date::GREGORIAN), Date.new(2001, 2, 3, Date::JULIAN), ].each do |x|