LibWeb: Validate operator count when parsing a calculation

Previously, we would allow calc values such as `calc(min(1 2))`, which
would be simplified to `calc(3)` because we assumed that numbers not
separated by an operator represented a sum. We now validate that the
number of operators we see is as we would expect before collecting
these values into a sum node.
This commit is contained in:
Tim Ledbetter 2025-06-30 14:07:30 +01:00 committed by Sam Atkins
parent 43778e9493
commit 78b6032940
Notes: github-actions[bot] 2025-07-02 09:14:07 +00:00
14 changed files with 646 additions and 12 deletions

View file

@ -4050,7 +4050,19 @@ RefPtr<CalculationNode const> Parser::parse_a_calculation(Vector<ComponentValue>
}
// Otherwise, replace values with a Sum node containing the value items of values as its children.
if (!single_value.has_value()) {
values.remove_all_matching([](CalcParsing::Node& value) { return value.has<CalcParsing::Operator>(); });
auto operator_count = 0u;
for (size_t i = 0; i < values.size();) {
auto& value = values[i];
if (value.has<CalcParsing::Operator>()) {
operator_count++;
values.remove(i);
} else {
i++;
}
}
if (values.size() == 0 || operator_count != values.size() - 1)
return nullptr;
single_value = make<CalcParsing::SumNode>(move(values));
}
}