LibWeb: Clamp interpolated values to the range of their numeric type

This fixes a UBSAN warning that we previously hit when interpolating
color values.
This commit is contained in:
Tim Ledbetter 2025-05-19 10:41:50 +01:00 committed by Sam Atkins
commit 09f4d90594
Notes: github-actions[bot] 2025-05-19 10:32:56 +00:00
4 changed files with 584 additions and 592 deletions

View file

@ -38,9 +38,13 @@ static T interpolate_raw(T from, T to, float delta)
{
if constexpr (AK::Detail::IsSame<T, double>) {
return from + (to - from) * static_cast<double>(delta);
} else {
return static_cast<AK::Detail::RemoveCVReference<T>>(from + (to - from) * delta);
} else if constexpr (AK::Detail::IsIntegral<T>) {
auto from_float = static_cast<float>(from);
auto to_float = static_cast<float>(to);
auto unclamped_result = from_float + (to_float - from_float) * delta;
return static_cast<AK::Detail::RemoveCVReference<T>>(clamp(unclamped_result, NumericLimits<T>::min(), NumericLimits<T>::max()));
}
return static_cast<AK::Detail::RemoveCVReference<T>>(from + (to - from) * delta);
}
static NonnullRefPtr<CSSStyleValue const> with_keyword_values_resolved(DOM::Element& element, PropertyID property_id, CSSStyleValue const& value)