From: Matthew Fernandez Date: Sat, 19 Feb 2022 18:21:02 +0000 (-0800) Subject: diffimg: handle allocation errors X-Git-Tag: 3.0.0~15^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6278a805d0df78105c2ecfce156baa607a9f4521;p=graphviz diffimg: handle allocation errors Previously these would be ignored resulting in crashes when allocations failed. --- diff --git a/contrib/diffimg/diffimg.c b/contrib/diffimg/diffimg.c index 3af05d2df..52ecb98bf 100644 --- a/contrib/diffimg/diffimg.c +++ b/contrib/diffimg/diffimg.c @@ -33,6 +33,7 @@ #define EX_DATAERR 65 #define EX_NOINPUT 66 #define EX_UNAVAILABLE 69 +#define EX_OSERR 71 #else #include #endif @@ -43,6 +44,15 @@ static char *pstopng="gs -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=- -q -"; +static void *xmalloc(size_t size) { + void *p = malloc(size); + if (size > 0 && p == NULL) { + fprintf(stderr, "Out of memory\n"); + graphviz_exit(EX_OSERR); + } + return p; +} + static gdImagePtr imageLoad (char *filename) { FILE *f; @@ -63,11 +73,11 @@ static gdImagePtr imageLoad (char *filename) } if (strcasecmp(ext, ".ps") == 0) { ext = ".png"; - tmp = malloc(strlen(filename) + strlen(ext) + 1); + tmp = xmalloc(strlen(filename) + strlen(ext) + 1); strcpy(tmp,filename); strcat(tmp,ext); - cmd = malloc(strlen(pstopng) + 2 + strlen(filename) + 2 + strlen(tmp) + 1); + cmd = xmalloc(strlen(pstopng) + 2 + strlen(filename) + 2 + strlen(tmp) + 1); strcpy(cmd,pstopng); strcat(cmd," <"); strcat(cmd,filename);