From: Matthew Fernandez Date: Fri, 8 Jul 2022 00:24:05 +0000 (-0700) Subject: patchwork nodecmp: rephrase comparator to avoid arithmetic X-Git-Tag: 5.0.1~32^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bcd684c3f21c6fba8cbff1658bace5032d35211f;p=graphviz patchwork nodecmp: rephrase comparator to avoid arithmetic cccb8b1d22a18031fc92d93133c7fa14ef7e1361 fixed an integer overflow in a `memcmp`-/`strcmp`-like comparator. The same situation exists in the code touched in this commit. Rather than wait for an edge case to expose an overflow here, this change makes the same update, removing arithmetic and the consequent possibility of overflow. --- diff --git a/lib/patchwork/patchwork.c b/lib/patchwork/patchwork.c index fb3d20101..22e2c6d1e 100644 --- a/lib/patchwork/patchwork.c +++ b/lib/patchwork/patchwork.c @@ -130,11 +130,13 @@ static treenode_t *mkTree (Agraph_t * g, attrsym_t* gp, attrsym_t* ap, attrsym_t static int nodecmp (treenode_t** p0, treenode_t** p1) { - double diff = (*p0)->area - (*p1)->area; - - if (diff < 0) return 1; - else if (diff > 0) return -1; - else return 0; + if ((*p0)->area < (*p1)->area) { + return 1; + } + if ((*p0)->area > (*p1)->area) { + return -1; + } + return 0; } static void layoutTree(treenode_t * tree)