From b22fd7c40d875b136693d53bcd36e756feef2c6d Mon Sep 17 00:00:00 2001 From: tompng Date: Sun, 5 Oct 2025 02:26:46 +0900 Subject: [PATCH 1/3] [ruby/json] Fix sliced string escaping https://github.com/ruby/json/commit/d7baf015d9 --- test/json/json_encoding_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/json/json_encoding_test.rb b/test/json/json_encoding_test.rb index 8dce81da590e76..2789e94b5b3f2b 100644 --- a/test/json/json_encoding_test.rb +++ b/test/json/json_encoding_test.rb @@ -35,6 +35,8 @@ def test_generate_shared_string # Ref: https://github.com/ruby/json/issues/859 s = "01234567890" assert_equal '"234567890"', JSON.dump(s[2..-1]) + s = '01234567890123456789"a"b"c"d"e"f"g"h' + assert_equal '"\"a\"b\"c\"d\"e\"f\"g\""', JSON.dump(s[20, 15]) end def test_unicode From f13e68e252ee96ee01e3b6eb11ad4109d5e033b1 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 21:18:53 +0900 Subject: [PATCH 2/3] [ruby/date] Do not repeat conversions to string https://github.com/ruby/date/commit/159e1ebb7f https://github.com/ruby/date/commit/4f7b6c9b42 --- ext/date/date_core.c | 20 ++++++++++---------- test/date/test_date_parse.rb | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 457838e41f8d55..8a7859704bf00f 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -4464,11 +4464,11 @@ get_limit(VALUE opt) #define rb_category_warn(category, fmt) rb_warn(fmt) #endif -static void +static VALUE check_limit(VALUE str, VALUE opt) { size_t slen, limit; - if (NIL_P(str)) return; + if (NIL_P(str)) return str; StringValue(str); slen = RSTRING_LEN(str); limit = get_limit(opt); @@ -4476,6 +4476,7 @@ check_limit(VALUE str, VALUE opt) rb_raise(rb_eArgError, "string length (%"PRI_SIZE_PREFIX"u) exceeds the limit %"PRI_SIZE_PREFIX"u", slen, limit); } + return str; } static VALUE @@ -4484,8 +4485,7 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass) VALUE vstr, vcomp, hash, opt; argc = rb_scan_args(argc, argv, "11:", &vstr, &vcomp, &opt); - check_limit(vstr, opt); - StringValue(vstr); + vstr = check_limit(vstr, opt); if (!rb_enc_str_asciicompat_p(vstr)) rb_raise(rb_eArgError, "string should have ASCII compatible encoding"); @@ -4620,7 +4620,7 @@ date_s__iso8601(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - check_limit(str, opt); + str = check_limit(str, opt); return date__iso8601(str); } @@ -4690,7 +4690,7 @@ date_s__rfc3339(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - check_limit(str, opt); + str = check_limit(str, opt); return date__rfc3339(str); } @@ -4759,7 +4759,7 @@ date_s__xmlschema(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - check_limit(str, opt); + str = check_limit(str, opt); return date__xmlschema(str); } @@ -4828,7 +4828,7 @@ date_s__rfc2822(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - check_limit(str, opt); + str = check_limit(str, opt); return date__rfc2822(str); } @@ -4896,7 +4896,7 @@ date_s__httpdate(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - check_limit(str, opt); + str = check_limit(str, opt); return date__httpdate(str); } @@ -4965,7 +4965,7 @@ date_s__jisx0301(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - check_limit(str, opt); + str = check_limit(str, opt); return date__jisx0301(str); } diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index cc29771cf85b22..29e55977c56cfe 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -1304,4 +1304,20 @@ def test_length_limit assert_raise(ArgumentError) { Date._parse("Jan " + "9" * 1000000) } end + + def test_string_argument + s = '2001-02-03T04:05:06Z' + obj = Class.new(Struct.new(:to_str, :count)) do + def to_str + self.count +=1 + super + end + end.new(s, 0) + + all_assertions_foreach(nil, :_parse, :_iso8601, :_rfc3339, :_xmlschema) do |m| + obj.count = 0 + assert_not_equal({}, Date.__send__(m, obj)) + assert_equal(1, obj.count) + end + end end From e6188c45e1114be3be4955971464f1b39d567d71 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 6 Oct 2025 14:17:44 +0900 Subject: [PATCH 3/3] [ruby/date] `Date._parse` does not accept `nil` https://github.com/ruby/date/commit/545066ca28 --- ext/date/date_core.c | 13 ++++++------- test/date/test_date_parse.rb | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 8a7859704bf00f..e4021051731714 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -4468,7 +4468,6 @@ static VALUE check_limit(VALUE str, VALUE opt) { size_t slen, limit; - if (NIL_P(str)) return str; StringValue(str); slen = RSTRING_LEN(str); limit = get_limit(opt); @@ -4620,7 +4619,7 @@ date_s__iso8601(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - str = check_limit(str, opt); + if (!NIL_P(str)) str = check_limit(str, opt); return date__iso8601(str); } @@ -4690,7 +4689,7 @@ date_s__rfc3339(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - str = check_limit(str, opt); + if (!NIL_P(str)) str = check_limit(str, opt); return date__rfc3339(str); } @@ -4759,7 +4758,7 @@ date_s__xmlschema(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - str = check_limit(str, opt); + if (!NIL_P(str)) str = check_limit(str, opt); return date__xmlschema(str); } @@ -4828,7 +4827,7 @@ date_s__rfc2822(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - str = check_limit(str, opt); + if (!NIL_P(str)) str = check_limit(str, opt); return date__rfc2822(str); } @@ -4896,7 +4895,7 @@ date_s__httpdate(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - str = check_limit(str, opt); + if (!NIL_P(str)) str = check_limit(str, opt); return date__httpdate(str); } @@ -4965,7 +4964,7 @@ date_s__jisx0301(int argc, VALUE *argv, VALUE klass) VALUE str, opt; rb_scan_args(argc, argv, "1:", &str, &opt); - str = check_limit(str, opt); + if (!NIL_P(str)) str = check_limit(str, opt); return date__jisx0301(str); } diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index 29e55977c56cfe..720624c02e338b 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -544,6 +544,8 @@ def test__parse__2 h = Date._parse('') assert_equal({}, h) + + assert_raise(TypeError) {Date._parse(nil)} end def test_parse