From: Matthew Fernandez Date: Sat, 30 Apr 2022 23:44:22 +0000 (-0700) Subject: cgraph: replace unchecked agxbuf allocations with alloc helpers X-Git-Tag: 4.0.0~45^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3b3b5fa7e86de571787c3e83a94af1e9917d3e4e;p=graphviz 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. --- 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; }