LibWeb/HTML: Ensure the year of month and week values is greater than 0

The validation algorithm for month and week inputs now also checks if
the year is greater than zero.
This commit is contained in:
Glenn Skrzypczak 2025-08-10 17:55:41 +02:00 committed by Tim Flynn
commit 57376f8701
Notes: github-actions[bot] 2025-08-14 15:07:09 +00:00
2 changed files with 9 additions and 16 deletions

View file

@ -51,9 +51,9 @@ bool is_valid_week_string(Utf16View const& value)
if (parts[1].length_in_code_units() != 3)
return false;
for (auto digit : parts[0])
if (!is_ascii_digit(digit))
return false;
auto year = parts[0].to_number<u64>();
if (!year.has_value() || *year == 0)
return false;
if (!parts[1].starts_with('W'))
return false;
@ -62,14 +62,8 @@ bool is_valid_week_string(Utf16View const& value)
if (!is_ascii_digit(parts[1].code_unit_at(2)))
return false;
u64 year = 0;
for (auto d : parts[0]) {
year *= 10;
year += parse_ascii_digit(d);
}
auto week = (parse_ascii_digit(parts[1].code_unit_at(1)) * 10) + parse_ascii_digit(parts[1].code_unit_at(2));
return week >= 1 && week <= week_number_of_the_last_day(year);
return week >= 1 && week <= week_number_of_the_last_day(*year);
}
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string
@ -90,9 +84,9 @@ bool is_valid_month_string(Utf16View const& value)
if (parts[1].length_in_code_units() != 2)
return false;
for (auto digit : parts[0])
if (!is_ascii_digit(digit))
return false;
auto year = parts[0].to_number<u64>();
if (!year.has_value() || *year == 0)
return false;
if (!is_ascii_digit(parts[1].code_unit_at(0)))
return false;