File ImageMagick-CVE-2017-12664.patch of Package ImageMagick.8212

Index: ImageMagick-6.8.8-1/coders/palm.c
===================================================================
--- ImageMagick-6.8.8-1.orig/coders/palm.c	2018-01-31 15:22:02.786181808 +0100
+++ ImageMagick-6.8.8-1/coders/palm.c	2018-01-31 15:33:21.017236328 +0100
@@ -857,13 +857,25 @@ static MagickBooleanType WritePALMImage(
       (void) WriteBlobMSBShort(image,0);  /* fill in size later */
     lastrow=(unsigned char *) NULL;
     if (image_info->compression == FaxCompression)
-      lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
-        sizeof(*lastrow));
+      {
+        lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
+          sizeof(*lastrow));
+        if (lastrow == (unsigned char *) NULL)
+        {
+          quantize_info=DestroyQuantizeInfo(quantize_info);
+          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+        }
+      }
       /* TODO check whether memory really was acquired? */
     one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row,
       sizeof(*one_row));
     if (one_row == (unsigned char *) NULL)
-      ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+      {
+        if (image_info->compression == FaxCompression)
+          lastrow=(unsigned char *) RelinquishMagickMemory(lastrow);
+        quantize_info=DestroyQuantizeInfo(quantize_info);
+        ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+      }
     for (y=0; y < (ssize_t) image->rows; y++)
     {
       ptr=one_row;
Index: ImageMagick-6.8.8-1/magick/quantize.c
===================================================================
--- ImageMagick-6.8.8-1.orig/magick/quantize.c	2013-12-22 02:50:17.000000000 +0100
+++ ImageMagick-6.8.8-1/magick/quantize.c	2018-01-31 15:39:42.927480707 +0100
@@ -3334,8 +3334,11 @@ static MagickBooleanType SetGrayscaleIma
   colormap_index=(ssize_t *) AcquireQuantumMemory(MaxMap+1,
     sizeof(*colormap_index));
   if (colormap_index == (ssize_t *) NULL)
-    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
-      image->filename);
+    {
+      colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
+      ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
+        image->filename);
+    }
   if (image->storage_class != PseudoClass)
     {
       ExceptionInfo
@@ -3344,8 +3347,11 @@ static MagickBooleanType SetGrayscaleIma
       for (i=0; i <= (ssize_t) MaxMap; i++)
         colormap_index[i]=(-1);
       if (AcquireImageColormap(image,MaxMap+1) == MagickFalse)
-        ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
-          image->filename);
+        {
+          colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
+          ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
+            image->filename);
+        }
       image->colors=0;
       status=MagickTrue;
       exception=(&image->exception);
@@ -3410,8 +3416,11 @@ static MagickBooleanType SetGrayscaleIma
   colormap=(PixelPacket *) AcquireQuantumMemory(image->colors,
     sizeof(*colormap));
   if (colormap == (PixelPacket *) NULL)
-    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
-      image->filename);
+    {
+      colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
+      ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
+        image->filename);
+    }
   j=0;
   colormap[j]=image->colormap[0];
   for (i=0; i < (ssize_t) image->colors; i++)