Index: gd-2.0.36.RC1/gd_gif_out.c
===================================================================
--- gd-2.0.36.RC1.orig/gd_gif_out.c	2019-01-31 10:46:53.252254713 +0100
+++ gd-2.0.36.RC1/gd_gif_out.c	2019-01-31 10:56:18.051410872 +0100
@@ -99,12 +99,18 @@ static void cl_hash (register count_int
 static void char_init (GifCtx *ctx);
 static void char_out (int c, GifCtx *ctx);
 static void flush_char (GifCtx *ctx);
+
+static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out);
+
 BGD_DECLARE(void *) gdImageGifPtr (gdImagePtr im, int *size)
 {
   void *rv;
   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
-  gdImageGifCtx (im, out);
-  rv = gdDPExtractData (out, size);
+  if (!_gdImageGifCtx(im, out)) {
+          rv = gdDPExtractData(out, size);
+  } else {
+          rv = NULL;
+  }
   out->gd_free (out);
   return rv;
 }
@@ -118,6 +124,12 @@ BGD_DECLARE(void) gdImageGif (gdImagePtr
 
 BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
 {
+        _gdImageGifCtx(im, out);
+}
+
+/* returns 0 on success, 1 on failure */
+static int _gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out)
+{
 	gdImagePtr pim = 0, tim = im;
 	int interlace, transparent, BitsPerPixel;
 	interlace = im->interlace;
@@ -128,7 +140,7 @@ BGD_DECLARE(void) gdImageGifCtx(gdImageP
 			based temporary image. */
 		pim = gdImageCreatePaletteFromTrueColor(im, 1, 256);
 		if (!pim) {
-			return;
+			return 1;
 		}
 		tim = pim; 
 	}
@@ -141,6 +153,7 @@ BGD_DECLARE(void) gdImageGifCtx(gdImageP
 		/* Destroy palette based temporary image. */
 		gdImageDestroy(	pim);
 	}
+        return 0;
 }
 
 BGD_DECLARE(void *) gdImageGifAnimBeginPtr (gdImagePtr im, int *size, int GlobalCM, int Loops)
Index: gd-2.0.36.RC1/gd_jpeg.c
===================================================================
--- gd-2.0.36.RC1.orig/gd_jpeg.c	2007-11-27 09:30:34.000000000 +0100
+++ gd-2.0.36.RC1/gd_jpeg.c	2019-01-31 10:59:48.616577030 +0100
@@ -84,6 +84,8 @@ fatal_jpeg_error (j_common_ptr cinfo)
   exit (99);
 }
 
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality);
+
 /*
  * Write IM to OUTFILE as a JFIF-formatted JPEG image, using quality
  * QUALITY.  If QUALITY is in the range 0-100, increasing values
@@ -103,8 +105,11 @@ BGD_DECLARE(void *) gdImageJpegPtr (gdIm
 {
   void *rv;
   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
-  gdImageJpegCtx (im, out, quality);
-  rv = gdDPExtractData (out, size);
+  if (!_gdImageJpegCtx(im, out, quality)) {
+          rv = gdDPExtractData(out, size);
+  } else {
+          rv = NULL;
+  }
   out->gd_free (out);
   return rv;
 }
@@ -113,6 +118,12 @@ void jpeg_gdIOCtx_dest (j_compress_ptr c
 
 BGD_DECLARE(void) gdImageJpegCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
 {
+	_gdImageJpegCtx(im, outfile, quality);
+}
+
+/* returns 0 on success, 1 on failure */
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
+{
   struct jpeg_compress_struct cinfo;
   struct jpeg_error_mgr jerr;
   int i, j, jidx;
@@ -148,7 +159,7 @@ BGD_DECLARE(void) gdImageJpegCtx (gdImag
       /* we're here courtesy of longjmp */
       if (row)
 	gdFree (row);
-      return;
+      return 1;
     }
 
   cinfo.err->error_exit = fatal_jpeg_error;
@@ -182,7 +193,7 @@ BGD_DECLARE(void) gdImageJpegCtx (gdImag
       fprintf (stderr, "gd-jpeg: error: unable to allocate JPEG row "
 	       "structure: gdCalloc returns NULL\n");
       jpeg_destroy_compress (&cinfo);
-      return;
+      return 1;
     }
 
   rowptr[0] = row;
@@ -260,6 +271,7 @@ BGD_DECLARE(void) gdImageJpegCtx (gdImag
   jpeg_finish_compress (&cinfo);
   jpeg_destroy_compress (&cinfo);
   gdFree (row);
+  return 0;
 }
 
 BGD_DECLARE(gdImagePtr) gdImageCreateFromJpeg (FILE * inFile)
Index: gd-2.0.36.RC1/gd_wbmp.c
===================================================================
--- gd-2.0.36.RC1.orig/gd_wbmp.c	2019-01-31 10:46:53.352255273 +0100
+++ gd-2.0.36.RC1/gd_wbmp.c	2019-01-31 11:07:40.915192689 +0100
@@ -89,6 +89,7 @@ gd_getin (void *in)
   return (gdGetC ((gdIOCtx *) in));
 }
 
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
 
 /*      gdImageWBMPCtx
    **  --------------
@@ -101,6 +102,12 @@ gd_getin (void *in)
  */
 BGD_DECLARE(void) gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
 {
+       _gdImageWBMPCtx(image, fg, out);
+}
+
+/* returns 0 on success, 1 on failure */
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
+{
 
   int x, y, pos;
   Wbmp *wbmp;
@@ -111,7 +118,7 @@ BGD_DECLARE(void) gdImageWBMPCtx (gdImag
        createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL)
   {
     fprintf (stderr, "Could not create WBMP\n");
-    return;
+    return 1;
   }
 
   /* fill up the WBMP structure */
@@ -130,9 +137,14 @@ BGD_DECLARE(void) gdImageWBMPCtx (gdImag
 
   /* write the WBMP to a gd file descriptor */
   if (writewbmp (wbmp, &gd_putout, out))
+  {
+    freewbmp(wbmp);
     fprintf (stderr, "Could not save WBMP\n");
+    return 1;
+  }
   /* des submitted this bugfix: gdFree the memory. */
   freewbmp (wbmp);
+  return 0;
 }
 
 
@@ -226,8 +238,11 @@ gdImageWBMPPtr (gdImagePtr im, int *size
 {
   void *rv;
   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
-  gdImageWBMPCtx (im, fg, out);
-  rv = gdDPExtractData (out, size);
+  if (!_gdImageWBMPCtx(im, fg, out)) {
+          rv = gdDPExtractData(out, size);
+  } else {
+          rv = NULL;
+  }
   out->gd_free (out);
   return rv;
 }
