From d48316ce155b9710c1941f350511873369175df3 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 19 May 2024 14:24:52 +1200 Subject: [PATCH] LibWeb: Fix division by zero on a zero-width viewport SVG image We were previously crashing by a division by zero due to an aspect ratio of zero on https://comicbookshop.co.nz/ --- .../expected/zero-width-viewport-svg-image.txt | 13 +++++++++++++ .../Layout/input/zero-width-viewport-svg-image.html | 1 + .../Libraries/LibWeb/SVG/SVGDecodedImageData.cpp | 11 +++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 Tests/LibWeb/Layout/expected/zero-width-viewport-svg-image.txt create mode 100755 Tests/LibWeb/Layout/input/zero-width-viewport-svg-image.html diff --git a/Tests/LibWeb/Layout/expected/zero-width-viewport-svg-image.txt b/Tests/LibWeb/Layout/expected/zero-width-viewport-svg-image.txt new file mode 100644 index 00000000000..9640c8f0727 --- /dev/null +++ b/Tests/LibWeb/Layout/expected/zero-width-viewport-svg-image.txt @@ -0,0 +1,13 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x600 [BFC] children: not-inline + BlockContainer at (8,8) content-size 784x150 children: inline + frag 0 from ImageBox start: 0, length: 0, rect: [8,8 300x150] baseline: 150 + ImageBox at (8,8) content-size 300x150 children: not-inline + (SVG-as-image isolated context) + Viewport <#document> at (0,0) content-size 300x150 [BFC] children: inline + SVGSVGBox at (0,0) content-size 300x150 [SVG] children: not-inline + +ViewportPaintable (Viewport<#document>) [0,0 800x600] + PaintableWithLines (BlockContainer) [0,0 800x600] + PaintableWithLines (BlockContainer) [8,8 784x150] + ImagePaintable (ImageBox) [8,8 300x150] diff --git a/Tests/LibWeb/Layout/input/zero-width-viewport-svg-image.html b/Tests/LibWeb/Layout/input/zero-width-viewport-svg-image.html new file mode 100755 index 00000000000..79dddc9a2d4 --- /dev/null +++ b/Tests/LibWeb/Layout/input/zero-width-viewport-svg-image.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Userland/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp b/Userland/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp index fdb1f28be89..c06bf77ea85 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp @@ -155,9 +155,16 @@ Optional SVGDecodedImageData::intrinsic_aspect_ratio() const if (width.has_value() && height.has_value()) return *width / *height; - if (auto const& viewbox = m_root_element->view_box(); viewbox.has_value()) - return CSSPixels::nearest_value_for(viewbox->width) / CSSPixels::nearest_value_for(viewbox->height); + if (auto const& viewbox = m_root_element->view_box(); viewbox.has_value()) { + auto viewbox_width = CSSPixels::nearest_value_for(viewbox->width); + if (viewbox_width == 0) + return {}; + + auto viewbox_height = CSSPixels::nearest_value_for(viewbox->height); + + return viewbox_width / viewbox_height; + } return {}; }