diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 457838e41f8d55..e4021051731714 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -4464,11 +4464,10 @@ 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; StringValue(str); slen = RSTRING_LEN(str); limit = get_limit(opt); @@ -4476,6 +4475,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 +4484,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 +4619,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); + 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); - 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); - 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); - 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); - 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); - 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 cc29771cf85b22..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 @@ -1304,4 +1306,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 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