]> granicus.if.org Git - graphviz/commitdiff
ccomps process: more tightly scope temporary buffer usage
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 12 Mar 2022 19:32:25 +0000 (11:32 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 13 Mar 2022 20:08:51 +0000 (13:08 -0700)
This makes it harder to accidentally introduce a use-after-free in this code.

cmd/tools/ccomps.c

index eeda9f3a6c96adc5c91477b9752f7dfa5b4bb259..2cb8a207b5c78602f015c14cb15b1009aa6bc7c8 100644 (file)
@@ -762,7 +762,6 @@ bindGraphinfo (Agraph_t * g)
 static int process(Agraph_t * g, char* graphName)
 {
     long n_cnt, c_cnt, e_cnt;
-    char *name;
     Agraph_t *out;
     Agnode_t *n;
     int extracted = 0;
@@ -782,10 +781,12 @@ static int process(Agraph_t * g, char* graphName)
                    x_node, agnameof(g));
            return 1;
        }
-       name = xmalloc(sizeof(PFX1) + strlen(graphName));
-       sprintf(name, PFX1, graphName);
-       out = agsubg(g, name, 1);
-       free(name);
+       {
+           char *name = xmalloc(sizeof(PFX1) + strlen(graphName));
+           sprintf(name, PFX1, graphName);
+           out = agsubg(g, name, 1);
+           free(name);
+       }
        aginit(out, AGRAPH, "graphinfo", sizeof(Agraphinfo_t), TRUE);
        GD_cc_subg(out) = 1;
        n_cnt = dfs(g, n, out);
@@ -805,10 +806,12 @@ static int process(Agraph_t * g, char* graphName)
     for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
        if (ND_mark(n))
            continue;
-       name = xmalloc(sizeof(PFX2) + strlen(graphName) + 32);
-       sprintf(name, PFX2, graphName, c_cnt);
-       out = agsubg(g, name, 1);
-       free(name);
+       {
+           char *name = xmalloc(sizeof(PFX2) + strlen(graphName) + 32);
+           sprintf(name, PFX2, graphName, c_cnt);
+           out = agsubg(g, name, 1);
+           free(name);
+       }
        aginit(out, AGRAPH, "graphinfo", sizeof(Agraphinfo_t), TRUE);
        GD_cc_subg(out) = 1;
        n_cnt = dfs(g, n, out);