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