Skip to content
Closed
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
13 changes: 8 additions & 5 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -49998,22 +49998,25 @@ static int string_skip_until(const uint8_t *sp, int *pp, const char *stoplist) {
}

/* parse a numeric field (max_digits = 0 -> no maximum) */
/* return FALSE if the value overflows INT32_MAX */
static BOOL string_get_digits(const uint8_t *sp, int *pp, int *pval,
int min_digits, int max_digits)
{
int v = 0;
uint64_t v = 0;
int c, p = *pp, p_start;

p_start = p;
while ((c = sp[p]) >= '0' && c <= '9') {
v = v * 10 + c - '0';
if (v >= INT32_MAX)
return FALSE;
p++;
if (p - p_start == max_digits)
break;
}
if (p - p_start < min_digits)
return FALSE;
*pval = v;
*pval = (int)v;
*pp = p;
return TRUE;
}
Expand Down Expand Up @@ -50053,7 +50056,7 @@ static BOOL string_get_tzoffset(const uint8_t *sp, int *pp, int *tzp, BOOL stric
sgn = sp[p++];
if (sgn == '+' || sgn == '-') {
int n = p;
if (!string_get_digits(sp, &p, &hh, 1, 9))
if (!string_get_digits(sp, &p, &hh, 1, 0))
return FALSE;
n = p - n;
if (strict && n != 2 && n != 4)
Expand Down Expand Up @@ -50245,7 +50248,7 @@ static BOOL js_date_parse_otherstring(const uint8_t *sp,
*is_local = FALSE;
} else {
p++;
if (string_get_digits(sp, &p, &val, 1, 9)) {
if (string_get_digits(sp, &p, &val, 1, 0)) {
if (c == '-') {
if (val == 0)
return FALSE;
Expand All @@ -50256,7 +50259,7 @@ static BOOL js_date_parse_otherstring(const uint8_t *sp,
}
}
} else
if (string_get_digits(sp, &p, &val, 1, 9)) {
if (string_get_digits(sp, &p, &val, 1, 0)) {
if (string_skip_char(sp, &p, ':')) {
/* time part */
fields[3] = val;
Expand Down
20 changes: 18 additions & 2 deletions tests/test_builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,28 @@ function test_date()
// is not a valid instance of this format.
// Hence the fractional part after . should have 3 digits and how
// a different number of digits is handled is implementation defined.
// As of March, 2025 these tests match the results of v8 11.3
assert(Date.parse(""), NaN);
assert(Date.parse("13"), NaN);
assert(Date.parse("31"), NaN);
assert(Date.parse("1000"), -30610224000000);
assert(Date.parse("1969"), -31536000000);
assert(Date.parse("1970"), 0);
assert(Date.parse("2000"), 946684800000);
assert(Date.parse("9999"), 253370764800000);
assert(Date.parse("275761"), NaN);
assert(Date.parse("999999"), NaN);
assert(Date.parse("1000000000"), NaN);
assert(Date.parse("-271821"), NaN);
assert(Date.parse("-271820"), -8639977881600000);
assert(Date.parse("-100000"), -3217862419200000);
assert(Date.parse("+100000"), 3093527980800000);
assert(Date.parse("+275760"), 8639977881600000);
assert(Date.parse("+275761"), NaN);
assert(Date.parse("2000-01"), 946684800000);
assert(Date.parse("2000-01-01"), 946684800000);
//assert(Date.parse("2000-01-01T"), NaN);
//assert(Date.parse("2000-01-01T00Z"), NaN);
assert(Date.parse("2000-01-01T"), NaN);
assert(Date.parse("2000-01-01T00Z"), NaN);
assert(Date.parse("2000-01-01T00:00Z"), 946684800000);
assert(Date.parse("2000-01-01T00:00:00Z"), 946684800000);
assert(Date.parse("2000-01-01T00:00:00.1Z"), 946684800100);
Expand Down
Loading