Index: GraphicsMagick-1.2.5/coders/dcm.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/dcm.c	2008-06-11 02:07:38.000000000 +0200
+++ GraphicsMagick-1.2.5/coders/dcm.c	2017-05-09 12:35:19.953494595 +0200
@@ -2664,6 +2664,13 @@ static unsigned int IsDCM(const unsigned
 */
 static Image *ReadDCMImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
+#define ThrowDCMReaderException(exception,message,image) \
+{ \
+  if (data != (unsigned char *) NULL) \
+    MagickFreeMemory(data); \
+  ThrowReaderException(exception,message,image); \
+}
+
   char
     explicit_vr[MaxTextExtent],
     implicit_vr[MaxTextExtent],
@@ -2747,10 +2754,11 @@ static Image *ReadDCMImage(const ImageIn
   /*
     Read DCM preamble.
   */
+  data=(unsigned char *) NULL;
   if ((count=ReadBlob(image,128,(char *) magick)) != 128)
-    ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+    ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
   if ((count=ReadBlob(image,4,(char *) magick)) != 4)
-     ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+     ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
   if (image->logging)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                           "magick: \"%.4s\"",magick);
@@ -2787,7 +2795,7 @@ static Image *ReadDCMImage(const ImageIn
     element=ReadBlobLSBShort(image);
     quantum=0;
     if (EOFBlob(image))
-       ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+       ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
     /*
       Find corresponding VR for this group and element.
     */
@@ -2797,7 +2805,7 @@ static Image *ReadDCMImage(const ImageIn
         break;
     (void) strlcpy(implicit_vr,dicom_info[i].vr,MaxTextExtent);
     if ((count=ReadBlob(image,2,(char *) explicit_vr)) != 2)
-       ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+       ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
     /*
       Check for "explicitness", but meta-file headers always explicit.
     */
@@ -2824,7 +2832,7 @@ static Image *ReadDCMImage(const ImageIn
           {
             (void) ReadBlobLSBShort(image);
             if (EOFBlob(image))
-              ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+              ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
             quantum=4;
           }
       }
@@ -2833,13 +2841,13 @@ static Image *ReadDCMImage(const ImageIn
       {
         datum=(long) ReadBlobLSBLong(image);
         if (EOFBlob(image))
-          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+          ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
       }
     else if (quantum == 2)
       {
         datum=(long) ReadBlobLSBShort(image);
         if (EOFBlob(image))
-          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+          ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
       }
     quantum=0;
     length=1;
@@ -2900,19 +2908,19 @@ static Image *ReadDCMImage(const ImageIn
     if ((length == 1) && (quantum == 1))
       {
         if ((datum=ReadBlobByte(image)) == EOF)
-          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+          ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
       }
     else if ((length == 1) && (quantum == 2))
       {
         datum=ReadBlobLSBShort(image);
         if (EOFBlob(image))
-          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+          ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
       }
     else if ((length == 1) && (quantum == 4))
       {
         datum=(long) ReadBlobLSBLong(image);
         if (EOFBlob(image))
-          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+          ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
       }
     else if ((quantum != 0) && (length != 0))
       {
@@ -2920,13 +2928,13 @@ static Image *ReadDCMImage(const ImageIn
           size;
 
         if (length > ((~0UL)/quantum))
-          ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+          ThrowDCMReaderException(CorruptImageError,ImproperImageHeader,image);
         data=MagickAllocateArray(unsigned char *,(length+1),quantum);
         if (data == (unsigned char *) NULL)
-          ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+          ThrowDCMReaderException(ResourceLimitError,MemoryAllocationFailed,image);
         size=quantum*length;
         if (ReadBlob(image,size,(char *) data) != size)
-          ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+          ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
         data[size]=0;
       }
     switch (group)
@@ -2942,10 +2950,10 @@ static Image *ReadDCMImage(const ImageIn
             */
             (void) strlcpy(transfer_syntax,(char *) data,MaxTextExtent);
             if (strcmp(transfer_syntax,"1.2.840.10008.1.2.2") == 0)
-              ThrowReaderException(CoderError,MSBByteOrderNotSupported,
+              ThrowDCMReaderException(CoderError,MSBByteOrderNotSupported,
                 image);
             if (strcmp(transfer_syntax,"1.2.840.10008.1.2.5") == 0)
-              ThrowReaderException(CoderError,RLECompressionNotSupported,image);
+              ThrowDCMReaderException(CoderError,RLECompressionNotSupported,image);
             break;
           }
           default:
@@ -3146,7 +3154,7 @@ static Image *ReadDCMImage(const ImageIn
             graymap=MagickAllocateArray(unsigned short *,
                                                  colors,sizeof(unsigned short));
             if (graymap == (unsigned short *) NULL)
-              ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
+              ThrowDCMReaderException(ResourceLimitError,MemoryAllocationFailed,
                 image);
             for (i=0; i < (long) colors; i++)
               if (bytes_per_pixel == 1)
@@ -3166,7 +3174,7 @@ static Image *ReadDCMImage(const ImageIn
               Initialize colormap.
             */
             if (!AllocateImageColormap(image,length/2))
-              ThrowReaderException(ResourceLimitError,UnableToCreateColormap,
+              ThrowDCMReaderException(ResourceLimitError,UnableToCreateColormap,
                 image);
             p=data;
             for (i=0; i < (long) image->colors; i++)
@@ -3222,7 +3230,7 @@ static Image *ReadDCMImage(const ImageIn
     MagickFreeMemory(data);
   }
   if ((width == 0) || (height == 0))
-    ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+    ThrowDCMReaderException(CorruptImageError,ImproperImageHeader,image);
   if ((strcmp(transfer_syntax,"1.2.840.10008.1.2.4.50") == 0) ||
       (strcmp(transfer_syntax,"1.2.840.10008.1.2.4.70") == 0))
     {
@@ -3282,7 +3290,7 @@ static Image *ReadDCMImage(const ImageIn
       */
       scale=MagickAllocateArray(Quantum *,(max_value+1),sizeof(Quantum));
       if (scale == (Quantum *) NULL)
-        ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+        ThrowDCMReaderException(ResourceLimitError,MemoryAllocationFailed,image);
       for (i=0; i <= (long) max_value; i++)
         scale[i]=(Quantum) (((double) MaxRGB*i)/max_value+0.5);
     }
@@ -3296,7 +3304,7 @@ static Image *ReadDCMImage(const ImageIn
     image->rows=height;
     if ((image->colormap == (PixelPacket *) NULL) && (samples_per_pixel == 1))
       if (!AllocateImageColormap(image,max_value+1))
-        ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+        ThrowDCMReaderException(ResourceLimitError,MemoryAllocationFailed,image);
     if (image_info->ping)
       break;
     if ((samples_per_pixel > 1) && (image->interlace == PlaneInterlace))
@@ -3327,7 +3335,7 @@ static Image *ReadDCMImage(const ImageIn
               }
               if (EOFBlob(image))
                 {
-                  ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+                  ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
                   status=MagickFail;
                   break;
                 }
@@ -3454,7 +3462,7 @@ static Image *ReadDCMImage(const ImageIn
             q++;
             if (EOFBlob(image))
               {
-                ThrowReaderException(CorruptImageError,UnexpectedEndOfFile,image);
+                ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
                 status=MagickFail;
               }
           }
