From 2ca9982b918a70f37352b460b4a2f7f77714fa68 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 27 Aug 2023 19:17:02 -0400 Subject: [PATCH] rect contour: Avoid nans in corner cases The length of rect contours can be zero, so we much check before we divide. --- gsk/gskcontour.c | 12 ++++++++++-- gsk/gskpath.c | 1 + gsk/gskpathmeasure.c | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gsk/gskcontour.c b/gsk/gskcontour.c index 1bd67ba688..d64d35f381 100644 --- a/gsk/gskcontour.c +++ b/gsk/gskcontour.c @@ -1467,6 +1467,8 @@ gsk_circle_contour_new (const graphene_point_t *center, { GskCircleContour *self; + g_assert (radius > 0); + self = g_new0 (GskCircleContour, 1); self->contour.klass = &GSK_CIRCLE_CONTOUR_CLASS; @@ -1700,7 +1702,10 @@ gsk_rect_contour_get_closest_point (const GskContour *contour, if (gsk_rect_contour_closest_point (self, point, threshold, out_dist, &distance)) { result->idx = 1; - result->t = distance / self->length; + if (self->length == 0) + result->t = 0; + else + result->t = distance / self->length; return TRUE; } @@ -1961,7 +1966,10 @@ gsk_rect_contour_get_point (const GskContour *contour, const GskRectContour *self = (const GskRectContour *) contour; result->idx = 1; - result->t = CLAMP (distance / self->length, 0, 1); + if (self->length == 0) + result->t = 0; + else + result->t = CLAMP (distance / self->length, 0, 1); } static float diff --git a/gsk/gskpath.c b/gsk/gskpath.c index 67aa2bbd71..ada80b83d1 100644 --- a/gsk/gskpath.c +++ b/gsk/gskpath.c @@ -603,6 +603,7 @@ gsk_path_get_closest_point (GskPath *self, if (gsk_contour_get_closest_point (self->contours[i], point, threshold, res, &distance)) { found = TRUE; + g_assert (0 <= res->t && res->t <= 1); res->contour = i; threshold = distance; } diff --git a/gsk/gskpathmeasure.c b/gsk/gskpathmeasure.c index 2aced2d12f..cb94ae7cbe 100644 --- a/gsk/gskpathmeasure.c +++ b/gsk/gskpathmeasure.c @@ -285,6 +285,9 @@ gsk_path_measure_get_point (GskPathMeasure *self, contour = gsk_path_get_contour (self->path, i); gsk_contour_get_point (contour, self->measures[i].contour_data, distance, res); + + g_assert (0 <= res->t && res->t <= 1); + res->contour = i; return TRUE;