]> granicus.if.org Git - graphviz/commitdiff
fdpgen compoundEdges: fix memory leak
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 28 Dec 2022 22:45:30 +0000 (14:45 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 1 Jan 2023 00:32:15 +0000 (16:32 -0800)
When hitting either of the error conditions in the loop, the next iteration
would be begun without clearing or freeing the object list. The next call to
`objectList` would overwrite the still-allocated previous object list and it
would be lost.

lib/fdpgen/clusteredges.c

index 1313c579b8c2f07a6382624f948bac0fd333e1ff..06509eda034b8bbed5b8824a6516fb6a517048e7 100644 (file)
@@ -85,15 +85,6 @@ static void freeObjlist(objlist * l)
     }
 }
 
-/* resetObjlist:
- * Reset objlist so it can be reused, using
- * the same memory.
- */
-static void resetObjlist(objlist * l)
-{
-    l->cnt = 0;
-}
-
 /* makeClustObs:
  * Create an obstacle corresponding to a cluster's bbox.
  */
@@ -258,7 +249,6 @@ int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
     node_t *head;
     edge_t *e;
     edge_t *e0;
-    objlist *objl = NULL;
     vconfig_t *vconfig;
     int rv = 0;
 
@@ -268,13 +258,14 @@ int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
            if (n == head && ED_count(e)) {     /* self arc */
                makeSelfArcs(e, GD_nodesep(g));
            } else if (ED_count(e)) {
-               objl = objectList(e, pm);
+               objlist *objl = objectList(e, pm);
                assert(objl->cnt <= INT_MAX);
                if (Plegal_arrangement(objl->obs, (int)objl->cnt)) {
                    vconfig = Pobsopen(objl->obs, (int)objl->cnt);
                    if (!vconfig) {
                        agerr(AGWARN, "compoundEdges: could not construct obstacles - falling back to straight line edges\n");
                        rv = 1;
+                       freeObjlist(objl);
                        continue;
                    }
                }
@@ -290,6 +281,7 @@ int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
                                margin.x, margin.y, pm->x, pm->y);
                        rv = 1;
                    }
+                   freeObjlist(objl);
                    continue;
                }
 
@@ -302,10 +294,9 @@ int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
                    assert(objl->cnt <= INT_MAX);
                    makeSpline(e0, objl->obs, (int)objl->cnt, false);
                }
-               resetObjlist(objl);
+               freeObjlist(objl);
            }
        }
     }
-    freeObjlist(objl);
     return rv;
 }