From: Matthew Fernandez Date: Sat, 14 Jan 2023 23:38:40 +0000 (-0800) Subject: bcomps getName: remove long lived allocation X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b8930546e803e84988ebfe8f96bd797bf9af01f8;p=graphviz bcomps 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/bcomps.c b/cmd/tools/bcomps.c index 8312121a1..b17c76ab8 100644 --- a/cmd/tools/bcomps.c +++ b/cmd/tools/bcomps.c @@ -92,30 +92,24 @@ static char *blockName(agxbuf *xb, char *gname, int d) { */ static char *getName(int ng, int nb) { - char *name; - static char *buf; + agxbuf name = {0}; if (ng == 0 && nb == 0) - name = outfile; + agxbput(&name, outfile); else { - if (!buf) { - size_t sz = strlen(outfile) + 100; // enough to handle '__' - buf = gv_alloc(sz); - } if (suffix) { if (nb < 0) - sprintf(buf, "%s_%d_T.%s", path, ng, suffix); + agxbprint(&name, "%s_%d_T.%s", path, ng, suffix); else - sprintf(buf, "%s_%d_%d.%s", path, ng, nb, suffix); + agxbprint(&name, "%s_%d_%d.%s", path, ng, nb, suffix); } else { if (nb < 0) - sprintf(buf, "%s_%d_T", path, ng); + agxbprint(&name, "%s_%d_T", path, ng); else - sprintf(buf, "%s_%d_%d", path, ng, nb); + agxbprint(&name, "%s_%d_%d", path, ng, nb); } - name = buf; } - return name; + return agxbdisown(&name); } static void gwrite(Agraph_t * g, int ng, int nb) @@ -134,8 +128,10 @@ static void gwrite(Agraph_t * g, int ng, int nb) if (!outf) { fprintf(stderr, "Could not open %s for writing\n", name); perror("bcomps"); + free(name); graphviz_exit(1); } + free(name); agwrite(g, outf); fclose(outf); }