From 5a3ed65ad86527f2631d34aa66c4afb38c5ca325 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 27 Aug 2023 23:12:25 -0400 Subject: [PATCH] Improve precondition checks for path points Add a helper function for checking that a path point is valid for a path, and use it. --- gsk/gskpathbuilder.c | 6 ++---- gsk/gskpathmeasure.c | 5 ++--- gsk/gskpathpoint.c | 14 +++++--------- gsk/gskpathpointprivate.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/gsk/gskpathbuilder.c b/gsk/gskpathbuilder.c index f838ca80be..b73e9a6376 100644 --- a/gsk/gskpathbuilder.c +++ b/gsk/gskpathbuilder.c @@ -1401,10 +1401,8 @@ gsk_path_builder_add_segment (GskPathBuilder *self, g_return_if_fail (self != NULL); g_return_if_fail (path != NULL); - g_return_if_fail (start != NULL); - g_return_if_fail (end != NULL); - g_return_if_fail (start->contour < n_contours); - g_return_if_fail (end->contour < n_contours); + g_return_if_fail (gsk_path_point_valid (start, path)); + g_return_if_fail (gsk_path_point_valid (end, path)); current = self->current_point; diff --git a/gsk/gskpathmeasure.c b/gsk/gskpathmeasure.c index 1edcf7b491..73ceddb012 100644 --- a/gsk/gskpathmeasure.c +++ b/gsk/gskpathmeasure.c @@ -22,7 +22,7 @@ #include "gskpathmeasure.h" #include "gskpathbuilder.h" -#include "gskpathpoint.h" +#include "gskpathpointprivate.h" #include "gskcontourprivate.h" #include "gskpathprivate.h" @@ -312,9 +312,8 @@ gsk_path_point_get_distance (const GskPathPoint *point, const GskContour *contour; float contour_offset = 0; - g_return_val_if_fail (point != NULL, 0); g_return_val_if_fail (measure != NULL, 0); - g_return_val_if_fail (point->contour < measure->n_contours, 0); + g_return_val_if_fail (gsk_path_point_valid (point, measure->path), 0); contour = gsk_path_get_contour (measure->path, point->contour); diff --git a/gsk/gskpathpoint.c b/gsk/gskpathpoint.c index 5f4592b2ed..4c564dc125 100644 --- a/gsk/gskpathpoint.c +++ b/gsk/gskpathpoint.c @@ -21,7 +21,7 @@ #include -#include "gskpathpoint.h" +#include "gskpathpointprivate.h" #include "gskcontourprivate.h" #include "gdk/gdkprivate.h" @@ -156,10 +156,9 @@ gsk_path_point_get_position (const GskPathPoint *point, { const GskContour *contour; - g_return_if_fail (point != NULL); g_return_if_fail (path != NULL); + g_return_if_fail (gsk_path_point_valid (point, path)); g_return_if_fail (position != NULL); - g_return_if_fail (point->contour < gsk_path_get_n_contours (path)); contour = gsk_path_get_contour (path, point->contour), gsk_contour_get_position (contour, point, position); @@ -195,10 +194,9 @@ gsk_path_point_get_tangent (const GskPathPoint *point, { const GskContour *contour; - g_return_if_fail (point != NULL); g_return_if_fail (path != NULL); + g_return_if_fail (gsk_path_point_valid (point, path)); g_return_if_fail (tangent != NULL); - g_return_if_fail (point->contour < gsk_path_get_n_contours (path)); contour = gsk_path_get_contour (path, point->contour), gsk_contour_get_tangent (contour, point, direction, tangent); @@ -228,9 +226,8 @@ gsk_path_point_get_rotation (const GskPathPoint *point, { graphene_vec2_t tangent; - g_return_val_if_fail (point != NULL, 0); g_return_val_if_fail (path != NULL, 0); - g_return_val_if_fail (point->contour < gsk_path_get_n_contours (path), 0); + g_return_val_if_fail (gsk_path_point_valid (point, path), 0); gsk_path_point_get_tangent (point, path, direction, &tangent); @@ -279,9 +276,8 @@ gsk_path_point_get_curvature (const GskPathPoint *point, { const GskContour *contour; - g_return_val_if_fail (point != NULL, 0); g_return_val_if_fail (path != NULL, 0); - g_return_val_if_fail (point->contour < gsk_path_get_n_contours (path), 0); + g_return_val_if_fail (gsk_path_point_valid (point, path), 0); contour = gsk_path_get_contour (path, point->contour); return gsk_contour_get_curvature (contour, point, direction, center); diff --git a/gsk/gskpathpointprivate.h b/gsk/gskpathpointprivate.h index f778d129fc..82f120ff41 100644 --- a/gsk/gskpathpointprivate.h +++ b/gsk/gskpathpointprivate.h @@ -7,5 +7,33 @@ G_BEGIN_DECLS #define GSK_PATH_POINT_INIT(c,i,tt) ((GskPathPoint){ .contour=(c), .idx=(i), .t=(tt) }) +static inline gboolean +gsk_path_point_valid (const GskPathPoint *point, + GskPath *path) +{ + const GskContour *contour; + gsize n_ops; + + if (point == NULL) + return FALSE; + + if (path == NULL) + return TRUE; + + if (point->contour >= gsk_path_get_n_contours (path)) + return FALSE; + + contour = gsk_path_get_contour (path, point->contour); + n_ops = gsk_contour_get_n_ops (contour); + if ((n_ops > 1 && point->idx >= n_ops) || + (n_ops == 1 && point->idx > n_ops)) + return FALSE; + + if (point->t < 0 || point->t > 1) + return FALSE; + + return TRUE; +} + G_END_DECLS