Skip to content

Commit c98d85d

Browse files
committed
Verify all argument classes first
1 parent fa1dd0b commit c98d85d

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

ext/date/date_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4528,6 +4528,7 @@ date_s__strptime_internal(int argc, VALUE *argv, VALUE klass,
45284528
rb_scan_args(argc, argv, "11", &vstr, &vfmt);
45294529

45304530
StringValue(vstr);
4531+
if (argc > 1) StringValue(vfmt);
45314532
if (!rb_enc_str_asciicompat_p(vstr))
45324533
rb_raise(rb_eArgError,
45334534
"string should have ASCII compatible encoding");
@@ -4538,7 +4539,6 @@ date_s__strptime_internal(int argc, VALUE *argv, VALUE klass,
45384539
flen = strlen(default_fmt);
45394540
}
45404541
else {
4541-
StringValue(vfmt);
45424542
if (!rb_enc_str_asciicompat_p(vfmt))
45434543
rb_raise(rb_eArgError,
45444544
"format should have ASCII compatible encoding");

test/date/test_date_strptime.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,20 @@ def test_sz
517517
d = DateTime.strptime('9000 +0200', '%Q %z')
518518
assert_equal([1970, 1, 1, 2, 0, 9], [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
519519
assert_equal(Rational(2, 24), d.offset)
520-
521520
end
522521

522+
def test_format_modified
523+
str = " " * 100
524+
fmt = Struct.new(:str) {
525+
def to_str
526+
str << "2026-06-01" << " "*100
527+
" %F "
528+
end
529+
}.new(str)
530+
d = Date._strptime(str, fmt)
531+
assert_not_nil(d)
532+
assert_equal(2026, d[:year])
533+
assert_equal(6, d[:mon])
534+
assert_equal(1, d[:mday])
535+
end
523536
end

0 commit comments

Comments
 (0)