Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions ext/date/date_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4464,18 +4464,18 @@ 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);
if (slen > limit) {
rb_raise(rb_eArgError,
"string length (%"PRI_SIZE_PREFIX"u) exceeds the limit %"PRI_SIZE_PREFIX"u", slen, limit);
}
return str;
}

static VALUE
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions test/date/test_date_parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ def test__parse__2

h = Date._parse('')
assert_equal({}, h)

assert_raise(TypeError) {Date._parse(nil)}
end

def test_parse
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions test/json/json_encoding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down