[System]
You are a security patch backporting assistant for SUSE/openSUSE.

Given a CVE description and an upstream fix, adapt the patch to work with
the target SUSE package version. Output the backported patch in unified
diff format. If the upstream fix applies directly, output it unchanged.

[User]
## CVE: CVE-2016-9556

The IsPixelGray function in MagickCore/pixel-accessor.h in ImageMagick 7.0.3-8 allows remote attackers to cause a denial of service (out-of-bounds heap read) via a crafted image file.

## Package
GraphicsMagick

## Files to modify
- coders/palm.c
- GraphicsMagick-1.3.20.orig/coders/palm.c

## Upstream Fix
```diff
Index: GraphicsMagick-1.3.20/coders/palm.c
===================================================================
--- GraphicsMagick-1.3.20.orig/coders/palm.c	2013-03-10 00:19:31.000000000 +0100
+++ GraphicsMagick-1.3.20/coders/palm.c	2016-06-14 15:36:43.426268139 +0200
@@ -359,7 +359,7 @@ static unsigned int
 %
 %
 */
-static int FindColor(PixelPacket *pixel)
+static long FindColor(PixelPacket *pixel)
 {
   register long
     i;
@@ -510,7 +510,7 @@ static Image *ReadPALMImage(const ImageI
             for(i = 0; i < (long) count; i++)
               {
                 (void) ReadBlobByte(image);
-                index=255 - i;
+                index=(unsigned int)(255 - i);
                 VerifyColormapIndex(image,index);
                 image->colormap[index].red = ScaleCharToQuantum(ReadBlobByte(image));
                 image->colormap[index].green = ScaleCharToQuantum(ReadBlobByte(image));
@@ -532,7 +532,7 @@ static Image *ReadPALMImage(const ImageI
                 transpix.green = (unsigned char) (ReadBlobByte(image) * MaxRGB / 63);
                 transpix.blue = (unsigned char) (ReadBlobByte(image) * MaxRGB / 31);
               }
-            index=255 - i;
+            index=(unsigned int)(255 - i);
             VerifyColormapIndex(image,index);
             image->colormap[index].red = ScaleCharToQuantum(PalmPalette[i][0]);
             image->colormap[index].green = ScaleCharToQuantum(PalmPalette[i][1]);
@@ -554,13 +554,13 @@ static Image *ReadPALMImage(const ImageI
       (void) SetImageType(image, TrueColorType);
     }

-  one_row = MagickAllocateMemory(unsigned char *,bytes_per_row);
+  one_row = MagickAllocateMemory(unsigned char *,Max(bytes_per_row,2*image->columns));
   if (one_row == (unsigned char *) NULL)
     ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
   if (compressionType == PALM_COMPRESSION_SCANLINE)
-    lastrow = MagickAllocateMemory(unsigned char *,bytes_per_row);
+    lastrow = MagickAllocateMemory(unsigned char *,Max(bytes_per_row,2*image->columns));

-  mask = (1l << bits_per_pixel) - 1;
+  mask = (unsigned long) (1U << bits_per_pixel) - 1;

   for(y = 0; y < (long) image->rows; y++)
     {
@@ -585,7 +585,7 @@ static Image *ReadPALMImage(const ImageI
                 for (i = 0; i < (long) bytes_per_row; i += 8)
                   {
                     count = ReadBlobByte(image);
-                    byte = Min(bytes_per_row - i, 8);
+                    byte = (unsigned long) Min(bytes_per_row - i, 8);
                     for (bit = 0; bit < byte; bit++)
                       {
                         if (y == 0 || count & (1 << (7 - bit)))
@@ -761,9 +761,6 @@ ModuleExport void UnregisterPALMImage(vo
 */
 static unsigned int WritePALMImage(const ImageInfo *image_info,Image *image)
 {
-  int
-    y;
-
   QuantizeInfo
     quantize_info;

@@ -805,6 +802,9 @@ static unsigned int WritePALMImage(const
   ImageCharacteristics
     characteristics;

+  long
+    y;
+
   /*
     Open output image file.
   */
@@ -941,7 +941,7 @@ static unsigned int WritePALMImage(const
     ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image);

   transpix.red=transpix.green=transpix.blue=0;
-  for (y=0; y < (int) image->rows; y++)
+  for (y=0; y < (long) image->rows; y++)
     {
       ptr = one_row;
       (void) memset(ptr, 0, bytes_per_row);
@@ -951,7 +951,7 @@ static unsigned int WritePALMImage(const
       indexes=AccessMutableIndexes(image);
       if(bits_per_pixel == 16)
         {
-          for (x=0; x < (int) image->columns; x++)
+          for (x=0; x < (long) image->columns; x++)
             {
               color16 = (unsigned short) (((p->red * 31) / MaxRGB) << 11
                                           | ((p->green * 63) / MaxRGB) << 5
@@ -973,7 +973,7 @@ static unsigned int WritePALMImage(const
         {
           byte = 0x00;
           bit = (unsigned char) (8 - bits_per_pixel);
-          for (x=0; x < (int) image->columns; x++)
+          for (x=0; x < (long) image->columns; x++)
             {
               if(bits_per_pixel < 8) /* Make sure we use the entire colorspace for bits_per_pixel */
                 color = (unsigned char) (indexes[x] * ((1 << bits_per_pixel) - 1) /
```

Generate a backported patch for the SUSE target version.

[Assistant]
Index: GraphicsMagick-1.3.20/coders/palm.c
===================================================================
--- GraphicsMagick-1.3.20.orig/coders/palm.c	2013-03-10 00:19:31.000000000 +0100
+++ GraphicsMagick-1.3.20/coders/palm.c	2016-06-14 15:36:43.426268139 +0200
@@ -359,7 +359,7 @@ static unsigned int
 %
 %
 */
-static int FindColor(PixelPacket *pixel)
+static long FindColor(PixelPacket *pixel)
 {
   register long
     i;
@@ -510,7 +510,7 @@ static Image *ReadPALMImage(const ImageI
             for(i = 0; i < (long) count; i++)
               {
                 (void) ReadBlobByte(image);
-                index=255 - i;
+                index=(unsigned int)(255 - i);
                 VerifyColormapIndex(image,index);
                 image->colormap[index].red = ScaleCharToQuantum(ReadBlobByte(image));
                 image->colormap[index].green = ScaleCharToQuantum(ReadBlobByte(image));
@@ -532,7 +532,7 @@ static Image *ReadPALMImage(const ImageI
                 transpix.green = (unsigned char) (ReadBlobByte(image) * MaxRGB / 63);
                 transpix.blue = (unsigned char) (ReadBlobByte(image) * MaxRGB / 31);
               }
-            index=255 - i;
+            index=(unsigned int)(255 - i);
             VerifyColormapIndex(image,index);
             image->colormap[index].red = ScaleCharToQuantum(PalmPalette[i][0]);
             image->colormap[index].green = ScaleCharToQuantum(PalmPalette[i][1]);
@@ -554,13 +554,13 @@ static Image *ReadPALMImage(const ImageI
       (void) SetImageType(image, TrueColorType);
     }

-  one_row = MagickAllocateMemory(unsigned char *,bytes_per_row);
+  one_row = MagickAllocateMemory(unsigned char *,Max(bytes_per_row,2*image->columns));
   if (one_row == (unsigned char *) NULL)
     ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
   if (compressionType == PALM_COMPRESSION_SCANLINE)
-    lastrow = MagickAllocateMemory(unsigned char *,bytes_per_row);
+    lastrow = MagickAllocateMemory(unsigned char *,Max(bytes_per_row,2*image->columns));

-  mask = (1l << bits_per_pixel) - 1;
+  mask = (unsigned long) (1U << bits_per_pixel) - 1;

   for(y = 0; y < (long) image->rows; y++)
     {
@@ -585,7 +585,7 @@ static Image *ReadPALMImage(const ImageI
                 for (i = 0; i < (long) bytes_per_row; i += 8)
                   {
                     count = ReadBlobByte(image);
-                    byte = Min(bytes_per_row - i, 8);
+                    byte = (unsigned long) Min(bytes_per_row - i, 8);
                     for (bit = 0; bit < byte; bit++)
                       {
                         if (y == 0 || count & (1 << (7 - bit)))
@@ -761,9 +761,6 @@ ModuleExport void UnregisterPALMImage(vo
 */
 static unsigned int WritePALMImage(const ImageInfo *image_info,Image *image)
 {
-  int
-    y;
-
   QuantizeInfo
     quantize_info;

@@ -805,6 +802,9 @@ static unsigned int WritePALMImage(const
   ImageCharacteristics
     characteristics;

+  long
+    y;
+
   /*
     Open output image file.
   */
@@ -941,7 +941,7 @@ static unsigned int WritePALMImage(const
     ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image);

   transpix.red=transpix.green=transpix.blue=0;
-  for (y=0; y < (int) image->rows; y++)
+  for (y=0; y < (long) image->rows; y++)
     {
       ptr = one_row;
       (void) memset(ptr, 0, bytes_per_row);
@@ -951,7 +951,7 @@ static unsigned int WritePALMImage(const
       indexes=AccessMutableIndexes(image);
       if(bits_per_pixel == 16)
         {
-          for (x=0; x < (int) image->columns; x++)
+          for (x=0; x < (long) image->columns; x++)
             {
               color16 = (unsigned short) (((p->red * 31) / MaxRGB) << 11
                                           | ((p->green * 63) / MaxRGB) << 5
@@ -973,7 +973,7 @@ static unsigned int WritePALMImage(const
         {
           byte = 0x00;
           bit = (unsigned char) (8 - bits_per_pixel);
-          for (x=0; x < (int) image->columns; x++)
+          for (x=0; x < (long) image->columns; x++)
             {
               if(bits_per_pixel < 8) /* Make sure we use the entire colorspace for bits_per_pixel */
                 color = (unsigned char) (indexes[x] * ((1 << bits_per_pixel) - 1) /