Skip to content

Commit

Permalink
[ruby/date] Consider the length of string to parse
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu authored and matzbot committed Dec 18, 2022
1 parent 49dc424 commit df49bf8
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions ext/date/date_strptime.c
Expand Up @@ -60,15 +60,15 @@ num_pattern_p(const char *s)
#define NUM_PATTERN_P() num_pattern_p(&fmt[fi + 1])

static long
read_digits(const char *s, VALUE *n, size_t width)
read_digits(const char *s, size_t slen, VALUE *n, size_t width)
{
size_t l;

if (!width)
return 0;

l = 0;
while (ISDIGIT(s[l])) {
while (l < slen && ISDIGIT(s[l])) {
if (++l == width) break;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ do { \
#define READ_DIGITS(n,w) \
do { \
size_t l; \
l = read_digits(&str[si], &n, w); \
l = read_digits(&str[si], slen - si, &n, w); \
if (l == 0) \
fail(); \
si += l; \
Expand Down Expand Up @@ -156,6 +156,14 @@ date__strptime_internal(const char *str, size_t slen,
si = fi = 0;

while (fi < flen) {
if (isspace((unsigned char)fmt[fi])) {
while (si < slen && isspace((unsigned char)str[si]))
si++;
while (++fi < flen && isspace((unsigned char)fmt[fi]));
continue;
}

if (si >= slen) fail();

switch (fmt[fi]) {
case '%':
Expand Down Expand Up @@ -576,7 +584,7 @@ date__strptime_internal(const char *str, size_t slen,

b = rb_backref_get();
rb_match_busy(b);
m = f_match(pat, rb_usascii_str_new2(&str[si]));
m = f_match(pat, rb_usascii_str_new(&str[si], slen - si));

if (!NIL_P(m)) {
VALUE s, l, o;
Expand Down Expand Up @@ -608,22 +616,13 @@ date__strptime_internal(const char *str, size_t slen,
if (str[si] != '%')
fail();
si++;
if (fi < flen)
if (str[si] != fmt[fi])
if (fi < flen) {
if (si >= slen || str[si] != fmt[fi])
fail();
si++;
si++;
}
goto matched;
}
case ' ':
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
while (isspace((unsigned char)str[si]))
si++;
fi++;
break;
default:
ordinal:
if (str[si] != fmt[fi])
Expand Down

0 comments on commit df49bf8

Please sign in to comment.