From: Matthew Fernandez Date: Sun, 21 Aug 2022 19:40:55 +0000 (-0700) Subject: pathplan Pshortestpath: use longs instead of ints for index variables X-Git-Tag: 6.0.1~25^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d48cfb348858cd2990c9fdee55c29803f4f93729;p=graphviz pathplan Pshortestpath: use longs instead of ints for index variables Within this function, these indices are sometimes calculated using pointer arithmetic which has a long (technically `ptrdiff_t`) result. Using a long type for these variables squashes 2 -Wconversion warnings and reduces the likelihood of arithmetic overflows. In future, it is possible we should convert this code to `ptrdiff_t` instead. It is unclear if this would be an improvement, given interaction with the surrounding code and cases where these indices are set to literals like `-1`. Changing to `ptrdiff_t` would also be more invasive than just widening the value range as done by this change. On most contemporary platforms, `long` and `ptrdiff_t` are the same type, so perhaps the difference is academic. --- diff --git a/lib/pathplan/shortest.c b/lib/pathplan/shortest.c index 8f9bd9a47..4dafdd673 100644 --- a/lib/pathplan/shortest.c +++ b/lib/pathplan/shortest.c @@ -68,8 +68,8 @@ static int opn; static int triangulate(pointnlink_t **, int); static bool isdiagonal(int, int, pointnlink_t **, int); static int loadtriangle(pointnlink_t *, pointnlink_t *, pointnlink_t *); -static void connecttris(int, int); -static bool marktripath(int, int); +static void connecttris(long, long); +static bool marktripath(long, long); static void add2dq(int, pointnlink_t *); static void splitdq(int, int); @@ -78,7 +78,7 @@ static int finddqsplit(pointnlink_t *); static int ccw(Ppoint_t *, Ppoint_t *, Ppoint_t *); static bool intersects(Ppoint_t *, Ppoint_t *, Ppoint_t *, Ppoint_t *); static bool between(Ppoint_t *, Ppoint_t *, Ppoint_t *); -static int pointintri(int, Ppoint_t *); +static int pointintri(long, Ppoint_t *); static int growpnls(int); static int growtris(int); @@ -95,7 +95,7 @@ int Pshortestpath(Ppoly_t * polyp, Ppoint_t eps[2], Ppolyline_t * output) int pi, minpi; double minx; Ppoint_t p1, p2, p3; - int trii, trij, ftrii, ltrii; + long trii, trij, ftrii, ltrii; int ei; pointnlink_t epnls[2], *lpnlp, *rpnlp, *pnlp; triangle_t *trip; @@ -371,8 +371,7 @@ static int loadtriangle(pointnlink_t * pnlap, pointnlink_t * pnlbp, } /* connect a pair of triangles at their common edge (if any) */ -static void connecttris(int tri1, int tri2) -{ +static void connecttris(long tri1, long tri2) { triangle_t *tri1p, *tri2p; int ei, ej; @@ -390,8 +389,7 @@ static void connecttris(int tri1, int tri2) } /* find and mark path from trii, to trij */ -static bool marktripath(int trii, int trij) -{ +static bool marktripath(long trii, long trij) { int ei; if (tris[trii].mark) @@ -488,8 +486,7 @@ static bool between(Ppoint_t * pap, Ppoint_t * pbp, Ppoint_t * pcp) p2.x * p2.x + p2.y * p2.y <= p1.x * p1.x + p1.y * p1.y; } -static int pointintri(int trii, Ppoint_t * pp) -{ +static int pointintri(long trii, Ppoint_t *pp) { int ei, sum; for (ei = 0, sum = 0; ei < 3; ei++)