Skip to content

Commit

Permalink
LibWeb: Do not crash when Radial Gradient height is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
shlyakpavel committed Nov 19, 2024
1 parent 24a6fd3 commit 6d612b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,22 @@ CSSPixelSize RadialGradientStyleValue::resolve_size(Layout::Node const& node, CS
auto distance = corner_distance(corner);
if (m_properties.ending_shape == EndingShape::Ellipse) {
auto shape = get_shape();
auto aspect_ratio = shape.width() / shape.height();
CSSPixels height = shape.height();
CSSPixels width = shape.width();

// Prevent division by zero
// https://w3c.github.io/csswg-drafts/css-images/#degenerate-radials
if (height == 0) {
// Render as if the ending shape was an ellipse whose width was an arbitrary very large number and whose height
// was an arbitrary very small number greater than zero. This will make the gradient look like a solid-color image equal
// to the color of the last color-stop, or equal to the average color of the gradient if it’s repeating.
constexpr auto arbitrary_small_number = CSSPixels::smallest_positive_value();
constexpr auto arbitrary_large_number = CSSPixels::max();
return CSSPixelSize { arbitrary_large_number, arbitrary_small_number };
}

auto aspect_ratio = width / height;

auto p = corner - center;
auto radius_a = sqrt(p.y() * p.y() * aspect_ratio * aspect_ratio + p.x() * p.x());
auto radius_b = radius_a / aspect_ratio;
Expand Down
2 changes: 2 additions & 0 deletions Tests/LibWeb/Ref/expected/empty-radial-gradient-crash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<div style="background-color: red;">Should not crash</div>
3 changes: 3 additions & 0 deletions Tests/LibWeb/Ref/input/empty-radial-gradient-crash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html>
<link rel="match" href="../expected/empty-radial-gradient-crash.html" />
<div style="background-image: radial-gradient(ellipse closest-corner at 0px 0px, white, red);">Should not crash</div>

0 comments on commit 6d612b5

Please sign in to comment.