Skip to content

Commit

Permalink
Fixed integer overflow in Date.parse().
Browse files Browse the repository at this point in the history
Found by OSS-Fuzz and UndefinedSanitizer.
  • Loading branch information
xeioex committed Jun 10, 2024
1 parent 9ade27b commit 5ab2598
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/njs_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,10 @@ njs_date_string_parse(njs_value_t *date)
}
}

p = njs_date_number_parse(&tm[NJS_DATE_MSEC], p, end, ms_length);
if (njs_slow_path(p == NULL)) {
if (njs_slow_path(njs_date_number_parse(&tm[NJS_DATE_MSEC], p, end,
njs_min(ms_length, 3))
== NULL))
{
return NAN;
}

Expand All @@ -686,13 +688,10 @@ njs_date_string_parse(njs_value_t *date)

} else if (ms_length == 2) {
tm[NJS_DATE_MSEC] *= 10;

} else if (ms_length >= 4) {
for (ms_length -= 3; ms_length > 0; ms_length--) {
tm[NJS_DATE_MSEC] /= 10;
}
}

p += ms_length;

if (p < end) {
utc_off = njs_date_utc_offset_parse(p, end);
if (njs_slow_path(utc_off == -1)) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/njs_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16285,6 +16285,12 @@ static njs_unit_test_t njs_test[] =
{ njs_str("Date.parse('2011-06-24T06:01:02.6255555Z')"),
njs_str("1308895262625") },

{ njs_str("Date.parse('2011-06-24T06:01:02.625555555Z')"),
njs_str("1308895262625") },

{ njs_str("Date.parse('2011-06-24T06:01:02.62555555599999Z')"),
njs_str("1308895262625") },

{ njs_str("Date.parse('2011-06-24T06:01:02.625555Z5')"),
njs_str("NaN") },

Expand Down

0 comments on commit 5ab2598

Please sign in to comment.