浏览代码

LibWeb: Avoid division by zero when calculating SVG viewbox aspect ratio

Tim Ledbetter 1 年之前
父节点
当前提交
e9383b9c86

+ 1 - 0
Tests/LibWeb/Text/expected/SVG/svg-viewbox-zero-height.txt

@@ -0,0 +1 @@
+  PASS (didn't crash)

+ 10 - 0
Tests/LibWeb/Text/input/SVG/svg-viewbox-zero-height.html

@@ -0,0 +1,10 @@
+<script src="../include.js"></script>
+<svg xmlns="http://www.w3.org/2000/svg" id="svg-element"></svg>
+<script>
+    test(() => {
+        const svgElement = document.getElementById("svg-element");
+        svgElement.height = 1;
+        svgElement.setAttribute("viewBox", "0 0 1 0");
+        println("PASS (didn't crash)");
+    });
+</script>

+ 5 - 1
Userland/Libraries/LibWeb/Layout/SVGSVGBox.cpp

@@ -73,7 +73,11 @@ Optional<CSSPixelFraction> SVGSVGBox::calculate_intrinsic_aspect_ratio() const
         auto const& viewbox = dom_node().view_box().value();
 
         // 2. return viewbox.width / viewbox.height
-        return CSSPixels::nearest_value_for(viewbox.width) / CSSPixels::nearest_value_for(viewbox.height);
+        auto height = CSSPixels::nearest_value_for(viewbox.height);
+        if (height != 0)
+            return CSSPixels::nearest_value_for(viewbox.width) / CSSPixels::nearest_value_for(viewbox.height);
+
+        return {};
     }
 
     // 4. return null