From 6278a805d0df78105c2ecfce156baa607a9f4521 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 19 Feb 2022 10:21:02 -0800 Subject: [PATCH] diffimg: handle allocation errors Previously these would be ignored resulting in crashes when allocations failed. --- contrib/diffimg/diffimg.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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); -- 2.40.0