]> granicus.if.org Git - graphviz/commitdiff
bcomps: replace unchecked allocations with alloc helpers
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 28 May 2022 19:26:52 +0000 (12:26 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 4 Jun 2022 05:04:57 +0000 (22:04 -0700)
This addresses some instances where out-of-memory would go undetected, leading
to a crash and/or data corruption.

cmd/tools/bcomps.c

index b1638d6f3821e1aab25e8bd1c0087a765dd09a0f..3faeff8c098ce372459f4ad01d270eaf1f0dec13 100644 (file)
@@ -28,6 +28,7 @@
 #include <getopt.h>
 
 #include <stdlib.h>
+#include <cgraph/alloc.h>
 #include <cgraph/cgraph.h>
 #include <cgraph/exit.h>
 #include <cgraph/stack.h>
@@ -81,7 +82,7 @@ static char *blockName(char *gname, int d)
     size_t sz = strlen(gname) + 128;
     if (sz > bufsz) {
        free(buf);
-       buf = malloc(sz);
+       buf = gv_alloc(sz);
     }
 
     if (*gname == '%') /* anonymous graph */
@@ -107,7 +108,7 @@ static char *getName(int ng, int nb)
     else {
        if (!buf) {
            size_t sz = strlen(outfile) + 100; // enough to handle '_<g>_<b>'
-           buf = malloc(sz);
+           buf = gv_alloc(sz);
        }
        if (suffix) {
            if (nb < 0)
@@ -306,9 +307,7 @@ static void split(char *name)
     if (sfx) {
        size = sfx - name;
        suffix = sfx + 1;
-       path = malloc(size + 1);
-       strncpy(path, name, size);
-       *(path + size) = '\0';
+       path = gv_strndup(name, size);
     } else {
        path = name;
     }