From 89db2484c4c619202cb6b296d47884e4396ed938 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 30 Jul 2022 09:38:15 -0700 Subject: [PATCH] pathplan Pobsopen: use a 'size_t' when counting objects MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This squashes 3 -Wsign-conversion warnings and is generally closer to what we would like to do here. The “proper” fix is for fields like `vconfig_t.N` to become `size_t` instead of `int`. But unfortunately they are part of the public API, and it seems undesirable to break API for this. Note that an assumption previously implicit in this function, that all inputs had a non-negative polygon count, is now an explicit assertion. --- lib/pathplan/cvt.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/pathplan/cvt.c b/lib/pathplan/cvt.c index 3e1c9ce0f..b1654ec3b 100644 --- a/lib/pathplan/cvt.c +++ b/lib/pathplan/cvt.c @@ -8,9 +8,11 @@ * Contributors: Details at https://graphviz.org *************************************************************************/ +#include #include #include #include +#include #include typedef Ppoint_t ilcoord_t; @@ -31,7 +33,7 @@ static void gasp_print_bezier(Ppolyline_t * route); vconfig_t *Pobsopen(Ppoly_t ** obs, int n_obs) { vconfig_t *rv; - int poly_i, pt_i, i, n; + int poly_i, pt_i, i; int start, end; rv = malloc(sizeof(vconfig_t)); @@ -40,14 +42,20 @@ vconfig_t *Pobsopen(Ppoly_t ** obs, int n_obs) } /* get storage */ - n = 0; - for (poly_i = 0; poly_i < n_obs; poly_i++) - n += obs[poly_i]->pn; + size_t n = 0; + for (poly_i = 0; poly_i < n_obs; poly_i++) { + assert(obs[poly_i]->pn >= 0); + n += (size_t)obs[poly_i]->pn; + } + if (n > INT_MAX) { // will this overflow rv->N? + free(rv); + return NULL; + } rv->P = malloc(n * sizeof(Ppoint_t)); rv->start = malloc((n_obs + 1) * sizeof(int)); rv->next = malloc(n * sizeof(int)); rv->prev = malloc(n * sizeof(int)); - rv->N = n; + rv->N = (int)n; rv->Npoly = n_obs; // bail out if any above allocations failed -- 2.40.0