]> granicus.if.org Git - graphviz/commitdiff
pathplan Pobsopen: call 'calloc' instead of 'malloc' when allocating arrays
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 30 Jul 2022 16:47:54 +0000 (09:47 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 5 Aug 2022 13:52:10 +0000 (06:52 -0700)
This is the generally preferred way of doing this that avoids various pitfalls,
such as integer overflow during multiplication.

lib/pathplan/cvt.c

index aa1e7b52b0d2a3f6f6f3639aa0ee95ed84e0f779..27ef7b29bc6fd37d163b86aa66d97b003aaa505a 100644 (file)
@@ -51,11 +51,11 @@ vconfig_t *Pobsopen(Ppoly_t ** obs, int n_obs)
        free(rv);
        return NULL;
     }
-    rv->P = malloc(n * sizeof(Ppoint_t));
+    rv->P = calloc(n, sizeof(Ppoint_t));
     assert(n_obs >= 0);
-    rv->start = malloc(((size_t)n_obs + 1) * sizeof(int));
-    rv->next = malloc(n * sizeof(int));
-    rv->prev = malloc(n * sizeof(int));
+    rv->start = calloc((size_t)n_obs + 1, sizeof(int));
+    rv->next = calloc(n, sizeof(int));
+    rv->prev = calloc(n, sizeof(int));
     rv->N = (int)n;
     rv->Npoly = n_obs;