From 7465362fe792a14066ff6558a2bf72d9194f6375 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 26 Apr 2023 07:19:07 +0200 Subject: [PATCH] LibWeb: Clamp CSS z-index to the range of a 32-bit integer This appears to be consistent with other engines, and fixes many pages where we were misinterpreting super large z-index values as something else entirely. --- Userland/Libraries/LibWeb/CSS/StyleProperties.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index da2bf77e66..204c0b490e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -216,8 +216,16 @@ Optional StyleProperties::z_index() const auto value = property(CSS::PropertyID::ZIndex); if (value->has_auto()) return {}; - if (value->has_integer()) - return value->to_integer(); + if (value->has_integer()) { + // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines. + // NOTE: Casting between 32-bit float and 32-bit integer is finicky here, since INT32_MAX is not representable as a 32-bit float! + auto integer = value->to_integer(); + if (integer >= static_cast(NumericLimits::max())) + return NumericLimits::max(); + if (integer <= static_cast(NumericLimits::min())) + return NumericLimits::min(); + return static_cast(integer); + } return {}; }