From: Matthew Fernandez Date: Sat, 14 Jan 2023 23:38:40 +0000 (-0800) Subject: ccomps getName: remove long lived allocation X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=453d098db6a5ee9ea5cff99526538ad4ea57fde7;p=graphviz ccomps getName: remove long lived allocation This code is not on a hot path, so we can afford to use clearer scoping and ownership semantics. Gitlab: #1950 --- diff --git a/cmd/tools/ccomps.c b/cmd/tools/ccomps.c index e7ed8b3d9..866cb40d9 100644 --- a/cmd/tools/ccomps.c +++ b/cmd/tools/ccomps.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -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); }