]> granicus.if.org Git - graphviz/commitdiff
ccomps process: replace 'sprintf' with 'agxbprint'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 14 Jan 2023 23:52:55 +0000 (15:52 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 15 Jan 2023 17:51:26 +0000 (09:51 -0800)
This replaces some error prone manual calculation with a dynamic buffer that
handles the calculation of needed allocations. This also potentially saves
memory in cases of short strings where an agxbuf can use inline storage.

Gitlab: #1950

cmd/tools/ccomps.c

index c1eeaeb047c5942f858ea02e60bdc4b60c77acc0..4199993bc66d8125aab94e7499cad5a1dac5a7a7 100644 (file)
@@ -708,10 +708,10 @@ static int process(Agraph_t * g, char* graphName)
            return 1;
        }
        {
-           char *name = gv_alloc(sizeof(PFX1) + strlen(graphName));
-           sprintf(name, PFX1, graphName);
-           out = agsubg(g, name, 1);
-           free(name);
+           agxbuf name = {0};
+           agxbprint(&name, PFX1, graphName);
+           out = agsubg(g, agxbuse(&name), 1);
+           agxbfree(&name);
        }
        aginit(out, AGRAPH, "graphinfo", sizeof(Agraphinfo_t), TRUE);
        GD_cc_subg(out) = 1;
@@ -733,10 +733,10 @@ static int process(Agraph_t * g, char* graphName)
        if (ND_mark(n))
            continue;
        {
-           char *name = gv_alloc(sizeof(PFX2) + strlen(graphName) + 32);
-           sprintf(name, PFX2, graphName, c_cnt);
-           out = agsubg(g, name, 1);
-           free(name);
+           agxbuf name = {0};
+           agxbprint(&name, PFX2, graphName, c_cnt);
+           out = agsubg(g, agxbuse(&name), 1);
+           agxbfree(&name);
        }
        aginit(out, AGRAPH, "graphinfo", sizeof(Agraphinfo_t), TRUE);
        GD_cc_subg(out) = 1;