From c5769a7033b1acd5ff18f4fbb11f582809f77d47 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 15 Apr 2021 20:38:43 +0300 Subject: [PATCH] LibWeb: Check radius sign in CanvasRenderingContext2D::{arc, ellipse} As required by the specification: 'If either radiusX or radiusY are negative, then throw an "IndexSizeError" DOMException.' --- .../LibWeb/HTML/CanvasRenderingContext2D.cpp | 15 ++++++++++++--- .../LibWeb/HTML/CanvasRenderingContext2D.h | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 0a8bfa7ab2..da74eb1f50 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -204,13 +204,21 @@ void CanvasRenderingContext2D::quadratic_curve_to(float cx, float cy, float x, f m_path.quadratic_bezier_curve_to({ cx, cy }, { x, y }); } -void CanvasRenderingContext2D::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise) +DOM::ExceptionOr CanvasRenderingContext2D::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise) { - ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise); + if (radius < 0) + return DOM::IndexSizeError::create(String::formatted("The radius provided ({}) is negative.", radius)); + return ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise); } -void CanvasRenderingContext2D::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise) +DOM::ExceptionOr CanvasRenderingContext2D::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise) { + if (radius_x < 0) + return DOM::IndexSizeError::create(String::formatted("The major-axis radius provided ({}) is negative.", radius_x)); + + if (radius_y < 0) + return DOM::IndexSizeError::create(String::formatted("The minor-axis radius provided ({}) is negative.", radius_y)); + if ((!counter_clockwise && (end_angle - start_angle) >= M_TAU) || (counter_clockwise && (start_angle - end_angle) >= M_TAU)) { start_angle = 0; @@ -261,6 +269,7 @@ void CanvasRenderingContext2D::ellipse(float x, float y, float radius_x, float r m_path.elliptical_arc_to(end_point, { radius_x, radius_y }, rotation, delta_theta > M_PI, !counter_clockwise); m_path.close(); + return {}; } void CanvasRenderingContext2D::rect(float x, float y, float width, float height) diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index 53f5674b52..0903d2f474 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -75,8 +75,8 @@ public: void line_to(float x, float y); void quadratic_curve_to(float cx, float cy, float x, float y); - void arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise); - void ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise); + DOM::ExceptionOr arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise); + DOM::ExceptionOr ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise); void rect(float x, float y, float width, float height); void stroke();