[PATCH] `Date._<format>(nil)` should return an empty Hash
authorJean Boussier <jean.boussier@gmail.com>
Mon, 15 Nov 2021 10:37:40 +0000 (11:37 +0100)
committerUtkarsh Gupta <utkarsh@debian.org>
Sun, 5 Dec 2021 23:55:44 +0000 (23:55 +0000)
Fix: https://github.com/ruby/date/issues/39

This is how versions previous to 3.2.1 behaved and Active Support
currently rely on this behavior.

https://github.com/rails/rails/blob/90357af08048ef5076730505f6e7b14a81f33d0c/activesupport/lib/active_support/values/time_zone.rb#L383-L384

Any Rails application upgrading to date `3.2.1` might run into unexpected errors.

Gbp-Pq: Name CVE-2021-41817-followup.patch

ext/date/date_core.c
test/date/test_date_parse.rb

index aa0dc4e10ee37a903fa788eb19941268dfb43b66..e9add0526b8b58aefddf454f2e66c9031a7cefb5 100644 (file)
@@ -4305,6 +4305,9 @@ get_limit(VALUE opt)
 static void
 check_limit(VALUE str, VALUE opt)
 {
+    if (NIL_P(str)) return;
+    if (SYMBOL_P(str)) str = rb_sym2str(str);
+
     StringValue(str);
     size_t slen = RSTRING_LEN(str);
     size_t limit = get_limit(opt);
index ee5343163a7921dfef22b7ec769a19557212b787..330ba9fd7923d3fc7bf0737b96c93fcbb3701bf4 100644 (file)
@@ -824,6 +824,13 @@ class TestDateParse < Test::Unit::TestCase
 
     h = Date._iso8601('')
     assert_equal({}, h)
+
+    h = Date._iso8601(nil)
+    assert_equal({}, h)
+
+    h = Date._iso8601('01-02-03T04:05:06Z'.to_sym)
+    assert_equal([2001, 2, 3, 4, 5, 6, 0],
+      h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset))
   end
 
   def test__rfc3339
@@ -839,6 +846,13 @@ class TestDateParse < Test::Unit::TestCase
 
     h = Date._rfc3339('')
     assert_equal({}, h)
+
+    h = Date._rfc3339(nil)
+    assert_equal({}, h)
+
+    h = Date._rfc3339('2001-02-03T04:05:06Z'.to_sym)
+    assert_equal([2001, 2, 3, 4, 5, 6, 0],
+      h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset))
   end
 
   def test__xmlschema
@@ -921,6 +935,13 @@ class TestDateParse < Test::Unit::TestCase
 
     h = Date._xmlschema('')
     assert_equal({}, h)
+
+    h = Date._xmlschema(nil)
+    assert_equal({}, h)
+
+    h = Date._xmlschema('2001-02-03'.to_sym)
+    assert_equal([2001, 2, 3, nil, nil, nil, nil],
+      h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset))
   end
 
   def test__rfc2822
@@ -953,6 +974,13 @@ class TestDateParse < Test::Unit::TestCase
 
     h = Date._rfc2822('')
     assert_equal({}, h)
+
+    h = Date._rfc2822(nil)
+    assert_equal({}, h)
+
+    h = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 UT'.to_sym)
+    assert_equal([2001, 2, 3, 4, 5, 6, 0],
+      h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset))
   end
 
   def test__httpdate
@@ -973,6 +1001,13 @@ class TestDateParse < Test::Unit::TestCase
 
     h = Date._httpdate('')
     assert_equal({}, h)
+
+    h = Date._httpdate(nil)
+    assert_equal({}, h)
+
+    h = Date._httpdate('Sat, 03 Feb 2001 04:05:06 GMT'.to_sym)
+    assert_equal([2001, 2, 3, 4, 5, 6, 0],
+      h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset))
   end
 
   def test__jisx0301
@@ -1001,6 +1036,13 @@ class TestDateParse < Test::Unit::TestCase
 
     h = Date._jisx0301('')
     assert_equal({}, h)
+
+    h = Date._jisx0301(nil)
+    assert_equal({}, h)
+
+    h = Date._jisx0301('H13.02.03T04:05:06.07+0100'.to_sym)
+    assert_equal([2001, 2, 3, 4, 5, 6, 3600],
+      h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset))
   end
 
   def test_iso8601