f530d4
From f58c813f8afcd08acdd630f378cff1a5009655cc Mon Sep 17 00:00:00 2001
f530d4
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
f530d4
Date: Thu, 31 Jan 2019 16:02:19 +0000
f530d4
Subject: [PATCH] merge in fixes for libgd CVE-2019-6978
f530d4
f530d4
---
f530d4
 README                 |  5 +++++
f530d4
 configure.ac           |  2 +-
f530d4
 src/extra/gd/gd_jpeg.c | 21 +++++++++++++++++----
f530d4
 src/extra/gd/gd_wbmp.c | 24 ++++++++++++++++++++++--
f530d4
 4 files changed, 45 insertions(+), 7 deletions(-)
f530d4
f530d4
diff --git a/src/extra/gd/gd_jpeg.c b/src/extra/gd/gd_jpeg.c
f530d4
index 7e6dfbb..b270186 100644
f530d4
--- a/src/extra/gd/gd_jpeg.c
f530d4
+++ b/src/extra/gd/gd_jpeg.c
f530d4
@@ -72,6 +72,8 @@ fatal_jpeg_error (j_common_ptr cinfo)
f530d4
   exit (99);
f530d4
 }
f530d4
 
f530d4
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality);
f530d4
+
f530d4
 /*
f530d4
  * Write IM to OUTFILE as a JFIF-formatted JPEG image, using quality
f530d4
  * QUALITY.  If QUALITY is in the range 0-100, increasing values
f530d4
@@ -93,8 +95,12 @@ gdImageJpegPtr (gdImagePtr im, int *size, int quality)
f530d4
 {
f530d4
   void *rv;
f530d4
   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
f530d4
-  gdImageJpegCtx (im, out, quality);
f530d4
-  rv = gdDPExtractData (out, size);
f530d4
+  if (out == NULL) return NULL;
f530d4
+  if (!_gdImageJpegCtx(im, out, quality)) {
f530d4
+    rv = gdDPExtractData(out, size);
f530d4
+  } else {
f530d4
+    rv = NULL;
f530d4
+  }
f530d4
   out->free (out);
f530d4
   return rv;
f530d4
 }
f530d4
@@ -103,6 +109,12 @@ static void jpeg_gdIOCtx_dest (j_compress_ptr cinfo, gdIOCtx * outfile);
f530d4
 
f530d4
 void
f530d4
 gdImageJpegCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
f530d4
+{
f530d4
+  _gdImageJpegCtx(im, outfile, quality);
f530d4
+}
f530d4
+
f530d4
+/* returns 0 on success, 1 on failure */
f530d4
+static int _gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
f530d4
 {
f530d4
   struct jpeg_compress_struct cinfo;
f530d4
   struct jpeg_error_mgr jerr;
f530d4
@@ -139,7 +151,7 @@ gdImageJpegCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
f530d4
       /* we're here courtesy of longjmp */
f530d4
       if (row)
f530d4
 	gdFree (row);
f530d4
-      return;
f530d4
+      return 1;
f530d4
     }
f530d4
 
f530d4
   cinfo.err->error_exit = fatal_jpeg_error;
f530d4
@@ -173,7 +185,7 @@ gdImageJpegCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
f530d4
       fprintf (stderr, "gd-jpeg: error: unable to allocate JPEG row "
f530d4
 	       "structure: gdCalloc returns NULL\n");
f530d4
       jpeg_destroy_compress (&cinfo);
f530d4
-      return;
f530d4
+      return 1;
f530d4
     }
f530d4
 
f530d4
   rowptr[0] = row;
f530d4
@@ -254,6 +266,7 @@ error:
f530d4
 #endif
f530d4
   jpeg_destroy_compress (&cinfo);
f530d4
   gdFree (row);
f530d4
+  return 0;
f530d4
 }
f530d4
 
f530d4
 gdImagePtr
f530d4
diff --git a/src/extra/gd/gd_wbmp.c b/src/extra/gd/gd_wbmp.c
f530d4
index f1258da..4b27043 100644
f530d4
--- a/src/extra/gd/gd_wbmp.c
f530d4
+++ b/src/extra/gd/gd_wbmp.c
f530d4
@@ -85,6 +85,7 @@ gd_getin (void *in)
f530d4
   return (gdGetC ((gdIOCtx *) in));
f530d4
 }
f530d4
 
f530d4
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
f530d4
 
f530d4
 /*      gdImageWBMPCtx
f530d4
    **  --------------
f530d4
@@ -97,6 +98,12 @@ gd_getin (void *in)
f530d4
  */
f530d4
 void
f530d4
 gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
f530d4
+{
f530d4
+  _gdImageWBMPCtx(image, fg, out);
f530d4
+}
f530d4
+
f530d4
+/* returns 0 on success, 1 on failure */
f530d4
+static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
f530d4
 {
f530d4
 
f530d4
   int x, y, pos;
f530d4
@@ -107,7 +114,7 @@ gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
f530d4
   if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL)
f530d4
     {
f530d4
       fprintf (stderr, "Could not create WBMP\n");
f530d4
-      return;
f530d4
+      return 1;
f530d4
     }
f530d4
 
f530d4
   /* fill up the WBMP structure */
f530d4
@@ -126,9 +133,16 @@ gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
f530d4
 
f530d4
   /* write the WBMP to a gd file descriptor */
f530d4
   if (writewbmp (wbmp, &gd_putout, out))
f530d4
+  {
f530d4
     fprintf (stderr, "Could not save WBMP\n");
f530d4
+    freewbmp (wbmp);
f530d4
+    return 1;
f530d4
+  }
f530d4
+
f530d4
   /* des submitted this bugfix: gdFree the memory. */
f530d4
   freewbmp (wbmp);
f530d4
+
f530d4
+  return 0;
f530d4
 }
f530d4
 
f530d4
 
f530d4
@@ -214,8 +228,12 @@ gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
f530d4
 {
f530d4
   void *rv;
f530d4
   gdIOCtx *out = gdNewDynamicCtx (2048, NULL);
f530d4
-  gdImageWBMPCtx (im, fg, out);
f530d4
-  rv = gdDPExtractData (out, size);
f530d4
+  if (out == NULL) return NULL;
f530d4
+  if (!_gdImageWBMPCtx(im, fg, out)) {
f530d4
+    rv = gdDPExtractData(out, size);
f530d4
+  } else {
f530d4
+    rv = NULL;
f530d4
+  }
f530d4
   out->free (out);
f530d4
   return rv;
f530d4
 }
f530d4
-- 
f530d4
2.20.1
f530d4