]> granicus.if.org Git - graphviz/commitdiff
ccomps getName: remove long lived allocation
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 14 Jan 2023 23:38:40 +0000 (15:38 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 15 Jan 2023 17:51:26 +0000 (09:51 -0800)
This code is not on a hot path, so we can afford to use clearer scoping and
ownership semantics.

Gitlab: #1950

cmd/tools/ccomps.c

index e7ed8b3d9686554f9977dbc61e1f34a58022f770..866cb40d9ff72427218f41791fb6572d581dd655 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <cgraph/agxbuf.h>
 #include <cgraph/alloc.h>
 #include <cgraph/cgraph.h>
 #include <cgraph/stack.h>
@@ -293,22 +294,18 @@ static int nodeInduce(Agraph_t * g, Agraph_t * eg)
 
 static char *getName(void)
 {
-    char *name;
-    static char *buf = 0;
+    agxbuf name = {0};
 
     if (sufcnt == 0)
-       name = outfile;
+       agxbput(&name, outfile);
     else {
-       if (!buf)
-           buf = gv_alloc(strlen(outfile) + 20); // enough to handle '_number'
        if (suffix)
-           sprintf(buf, "%s_%d.%s", path, sufcnt, suffix);
+           agxbprint(&name, "%s_%d.%s", path, sufcnt, suffix);
        else
-           sprintf(buf, "%s_%d", path, sufcnt);
-       name = buf;
+           agxbprint(&name, "%s_%d", path, sufcnt);
     }
     sufcnt++;
-    return name;
+    return agxbuse(&name);
 }
 
 static void gwrite(Agraph_t * g)
@@ -325,8 +322,10 @@ static void gwrite(Agraph_t * g)
        if (!outf) {
            fprintf(stderr, "Could not open %s for writing\n", name);
            perror("ccomps");
+           free(name);
            graphviz_exit(EXIT_FAILURE);
        }
+       free(name);
        agwrite(g, outf);
        fclose(outf);
     }