Index: GraphicsMagick-1.2.5/coders/sun.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/sun.c	2016-06-16 14:36:22.269139510 +0200
+++ GraphicsMagick-1.2.5/coders/sun.c	2016-06-16 15:11:13.891468928 +0200
@@ -226,6 +226,7 @@ static Image *ReadSUNImage(const ImageIn
     bit;
 
   long
+    count,
     y;
 
   register IndexPacket
@@ -243,9 +244,6 @@ static Image *ReadSUNImage(const ImageIn
   register unsigned char
     *p;
 
-  size_t
-    count;
-
   SUNInfo
     sun_info;
 
@@ -290,13 +288,21 @@ static Image *ReadSUNImage(const ImageIn
     sun_info.type=ReadBlobMSBLong(image);
     sun_info.maptype=ReadBlobMSBLong(image);
     sun_info.maplength=ReadBlobMSBLong(image);
-    image->columns= sun_info.width;
-    image->rows= sun_info.height;
     extent=sun_info.height*sun_info.width;
     if ((sun_info.height != 0) && (sun_info.width != extent/sun_info.height))
       ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
     if (sun_info.depth == 0 || sun_info.depth > 32)
       ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+    if ((sun_info.type != RT_STANDARD) && (sun_info.type != RT_ENCODED) &&
+        (sun_info.type != RT_FORMAT_RGB))
+      ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+    if ((sun_info.maptype == RMT_NONE) && (sun_info.maplength != 0))
+      ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+    if ((sun_info.maptype != RMT_NONE) && (sun_info.maptype != RMT_EQUAL_RGB) &&
+        (sun_info.maptype != RMT_RAW))
+      ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+    image->columns=sun_info.width;
+    image->rows=sun_info.height;
     image->depth=sun_info.depth <= 8 ? 8 : QuantumDepth;
     if (sun_info.depth < 24)
       {
@@ -337,13 +343,19 @@ static Image *ReadSUNImage(const ImageIn
         if (sun_colormap == (unsigned char *) NULL)
           ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
             image);
-        (void) ReadBlob(image,image->colors,(char *) sun_colormap);
+        count=ReadBlob(image,image->colors,(char *) sun_colormap);
+        if (count != (long) image->colors)
+          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
         for (i=0; i < (long) image->colors; i++)
           image->colormap[i].red=ScaleCharToQuantum(sun_colormap[i]);
-        (void) ReadBlob(image,image->colors,(char *) sun_colormap);
+        count=ReadBlob(image,image->colors,(char *) sun_colormap);
+        if (count != (long) image->colors)
+          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
         for (i=0; i < (long) image->colors; i++)
           image->colormap[i].green=ScaleCharToQuantum(sun_colormap[i]);
-        (void) ReadBlob(image,image->colors,(char *) sun_colormap);
+        count=ReadBlob(image,image->colors,(char *) sun_colormap);
+        if (count != (long) image->colors)
+          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
         for (i=0; i < (long) image->colors; i++)
           image->colormap[i].blue=ScaleCharToQuantum(sun_colormap[i]);
         MagickFreeMemory(sun_colormap);
@@ -361,12 +373,14 @@ static Image *ReadSUNImage(const ImageIn
         if (sun_colormap == (unsigned char *) NULL)
           ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
             image);
-        (void) ReadBlob(image,sun_info.maplength,(char *) sun_colormap);
+        count=ReadBlob(image,sun_info.maplength,(char *) sun_colormap);
+        if (count != (long) sun_info.maplength)
+          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
         MagickFreeMemory(sun_colormap);
         break;
       }
       default:
-        ThrowReaderException(CoderError,ColormapTypeNotSupported,image)
+        break;
     } 
     image->matte=(sun_info.depth == 32);
     image->columns=sun_info.width;
@@ -383,7 +397,7 @@ static Image *ReadSUNImage(const ImageIn
     if (sun_data == (unsigned char *) NULL)
       ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
     count=ReadBlob(image,sun_info.length,(char *) sun_data);
-    if ((count == 0) && (sun_info.type != RT_ENCODED))
+    if (count != (long) sun_info.length)
       ThrowReaderException(CorruptImageError,UnableToReadImageData,image);
     sun_pixels=sun_data;
     bytes_per_line=0;
@@ -423,7 +437,7 @@ static Image *ReadSUNImage(const ImageIn
         }
         if ((image->columns % 8) != 0)
           {
-            for (bit=7; bit >= (long) (8-(image->columns % 8)); bit--)
+            for (bit=7; bit >= (int) (8-(image->columns % 8)); bit--)
               indexes[x+7-bit]=((*p) & (0x01 << bit) ? 0x01 : 0x00);
             p++;
           }
