From 3b3b5fa7e86de571787c3e83a94af1e9917d3e4e Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 30 Apr 2022 16:44:22 -0700 Subject: [PATCH] cgraph: replace unchecked agxbuf allocations with alloc helpers These allocations are at the core of some work Graphviz does and were not checked for failure. This change swaps confusing crashes on out-of-memory for a more graceful exit. --- lib/cgraph/agxbuf.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/cgraph/agxbuf.c b/lib/cgraph/agxbuf.c index 627e02b3f..eef13d632 100644 --- a/lib/cgraph/agxbuf.c +++ b/lib/cgraph/agxbuf.c @@ -14,8 +14,7 @@ #include #include #include - -#define N_GNEW(n,t) calloc((n),sizeof(t)) +#include /* agxbinit: * Assume if init is non-null, hint = sizeof(init[]) @@ -29,7 +28,7 @@ void agxbinit(agxbuf * xb, unsigned int hint, unsigned char *init) if (hint == 0) hint = BUFSIZ; xb->dyna = 1; - xb->buf = N_GNEW(hint, unsigned char); + xb->buf = gv_calloc(hint, sizeof(unsigned char)); } xb->eptr = xb->buf + hint; xb->ptr = xb->buf; @@ -52,9 +51,9 @@ void agxbmore(agxbuf * xb, size_t ssz) nsize = size + ssz; cnt = (size_t) (xb->ptr - xb->buf); if (xb->dyna) { - nbuf = realloc(xb->buf, nsize); + nbuf = gv_recalloc(xb->buf, size, nsize, sizeof(unsigned char)); } else { - nbuf = N_GNEW(nsize, unsigned char); + nbuf = gv_calloc(nsize, sizeof(unsigned char)); memcpy(nbuf, xb->buf, cnt); xb->dyna = 1; } -- 2.40.0