From 4ae3f7c81ba11796ac6bea0c457ac2ac78897b0d Mon Sep 17 00:00:00 2001 From: sammycage Date: Mon, 23 Sep 2024 12:16:29 +0100 Subject: [PATCH] Add null and zero division checks to avoid undefined behavior. --- source/graphics.cpp | 6 ------ source/svgelement.cpp | 6 +++--- source/svggeometryelement.cpp | 2 +- source/svgtextelement.cpp | 2 +- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/source/graphics.cpp b/source/graphics.cpp index aab5592..a13b56d 100644 --- a/source/graphics.cpp +++ b/source/graphics.cpp @@ -606,8 +606,6 @@ void Canvas::strokePath(const Path& path, const StrokeData& strokeData, const Tr void Canvas::fillText(const std::u32string_view& text, const Font& font, const Point& origin, const Transform& transform) { - if(text.empty() || font.isNull()) - return; plutovg_canvas_reset_matrix(m_canvas); plutovg_canvas_translate(m_canvas, -m_x, -m_y); plutovg_canvas_transform(m_canvas, &transform.matrix()); @@ -653,10 +651,6 @@ void Canvas::clipRect(const Rect& rect, FillRule clipRule, const Transform& tran void Canvas::drawImage(const Bitmap& image, const Rect& dstRect, const Rect& srcRect, const Transform& transform) { - if(dstRect.isEmpty() || srcRect.isEmpty()) { - return; - } - auto xScale = dstRect.w / srcRect.w; auto yScale = dstRect.h / srcRect.h; plutovg_matrix_t matrix = {xScale, 0, 0, yScale, -srcRect.x * xScale, -srcRect.y * yScale}; diff --git a/source/svgelement.cpp b/source/svgelement.cpp index c8b337e..61eee7d 100644 --- a/source/svgelement.cpp +++ b/source/svgelement.cpp @@ -812,12 +812,12 @@ Rect SVGImageElement::strokeBoundingBox() const void SVGImageElement::render(SVGRenderState& state) const { - if(isDisplayNone() || isVisibilityHidden()) - return; - if(!m_image.width() || !m_image.height()) + if(m_image.isNull() || isDisplayNone() || isVisibilityHidden()) return; Rect dstRect(fillBoundingBox()); Rect srcRect(0, 0, m_image.width(), m_image.height()); + if(dstRect.isEmpty() || srcRect.isEmpty()) + return; m_preserveAspectRatio.transformRect(dstRect, srcRect); SVGBlendInfo blendInfo(this); diff --git a/source/svggeometryelement.cpp b/source/svggeometryelement.cpp index 345750e..14819d5 100644 --- a/source/svggeometryelement.cpp +++ b/source/svggeometryelement.cpp @@ -138,7 +138,7 @@ void SVGGeometryElement::updateMarkerPositions(SVGMarkerPositionList& positions, void SVGGeometryElement::render(SVGRenderState& state) const { - if(isVisibilityHidden() || isDisplayNone()) + if(m_path.isNull() || isVisibilityHidden() || isDisplayNone()) return; SVGBlendInfo blendInfo(this); SVGRenderState newState(this, state, localTransform()); diff --git a/source/svgtextelement.cpp b/source/svgtextelement.cpp index 211d293..ed1292d 100644 --- a/source/svgtextelement.cpp +++ b/source/svgtextelement.cpp @@ -309,7 +309,7 @@ void SVGTextElement::layout(SVGLayoutState& state) void SVGTextElement::render(SVGRenderState& state) const { - if(isVisibilityHidden() || isDisplayNone()) + if(m_text.empty() || isVisibilityHidden() || isDisplayNone()) return; SVGBlendInfo blendInfo(this); SVGRenderState newState(this, state, localTransform());