Index: GraphicsMagick-1.2.5/coders/dcm.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/dcm.c	2017-12-04 16:30:52.741684832 +0100
+++ GraphicsMagick-1.2.5/coders/dcm.c	2017-12-04 17:50:44.834217741 +0100
@@ -2931,7 +2931,7 @@ static Image *ReadDCMImage(const ImageIn
           ThrowDCMReaderException(CorruptImageError,ImproperImageHeader,image);
         data=MagickAllocateArray(unsigned char *,(length+1),quantum);
         if (data == (unsigned char *) NULL)
-          ThrowDCMReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+          ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
         size=quantum*length;
         if (ReadBlob(image,size,(char *) data) != size)
           ThrowDCMReaderException(CorruptImageError,UnexpectedEndOfFile,image);
@@ -2948,6 +2948,8 @@ static Image *ReadDCMImage(const ImageIn
             /*
               Transfer Syntax.
             */
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) strlcpy(transfer_syntax,(char *) data,MaxTextExtent);
             if (strcmp(transfer_syntax,"1.2.840.10008.1.2.2") == 0)
               ThrowDCMReaderException(CoderError,MSBByteOrderNotSupported,
@@ -2967,6 +2969,8 @@ static Image *ReadDCMImage(const ImageIn
         {
           case 0x0020:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"StudyDate",(char *) data);
             break;
           }
@@ -2981,6 +2985,8 @@ static Image *ReadDCMImage(const ImageIn
         {
           case 0x0010:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"PatientName",(char *) data);
             break;
           }
@@ -2995,6 +3001,8 @@ static Image *ReadDCMImage(const ImageIn
         {
           case 0x1060:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"TriggerTime",(char *) data);
             break;
           }
@@ -3009,6 +3017,8 @@ static Image *ReadDCMImage(const ImageIn
         {
           case 0x101e:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"FieldOfView",(char *) data);
             break;
           }
@@ -3023,21 +3033,29 @@ static Image *ReadDCMImage(const ImageIn
         {
           case 0x0011:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"SeriesNumber",(char *) data);
             break;
           }
           case 0x0032:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"ImagePosition",(char *) data);
             break;
           }
           case 0x0037:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"ImageOrientation",(char *) data);
             break;
           }
           case 0x1041:
           {
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             (void) SetImageAttribute(image,"SliceLocation",(char *) data);
             break;
           }
@@ -3063,6 +3081,8 @@ static Image *ReadDCMImage(const ImageIn
             /*
               Photometric interpretation.
             */
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             for (i=0; i < (long) Min(length, MaxTextExtent-1); i++)
               photometric[i]=data[i];
             photometric[i]='\0';
@@ -3082,6 +3102,8 @@ static Image *ReadDCMImage(const ImageIn
             /*
               Number of frames.
             */
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             number_scenes=atoi((char *) data);
             break;
           }
@@ -3156,6 +3178,8 @@ static Image *ReadDCMImage(const ImageIn
             if (graymap == (unsigned short *) NULL)
               ThrowDCMReaderException(ResourceLimitError,MemoryAllocationFailed,
                 image);
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             for (i=0; i < (long) colors; i++)
               if (bytes_per_pixel == 1)
                 graymap[i]=data[i];
@@ -3173,6 +3197,8 @@ static Image *ReadDCMImage(const ImageIn
             /*
               Initialize colormap.
             */
+            if (data == NULL)
+              ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             if (!AllocateImageColormap(image,length/2))
               ThrowDCMReaderException(ResourceLimitError,UnableToCreateColormap,
                 image);
@@ -3198,8 +3224,10 @@ static Image *ReadDCMImage(const ImageIn
     }
     if (image_info->verbose)
       {
-        if (data == (unsigned char *) NULL)
-          (void) fprintf(stdout,"%ld\n",datum);
+        if (data == NULL)
+        {
+          ThrowDCMReaderException(CorruptImageError,InsufficientImageDataInFile,image);
+        }
         else
           {
             /*
@@ -3227,7 +3255,8 @@ static Image *ReadDCMImage(const ImageIn
             (void) fprintf(stdout,"\n");
           }
       }
-    MagickFreeMemory(data);
+    if (data)
+      MagickFreeMemory(data);
   }
   if ((width == 0) || (height == 0))
     ThrowDCMReaderException(CorruptImageError,ImproperImageHeader,image);
@@ -3522,9 +3551,11 @@ static Image *ReadDCMImage(const ImageIn
   */
   if (scale != (Quantum *) NULL)
     MagickFreeMemory(scale);
-  while (image->previous != (Image *) NULL)
-    image=image->previous;
-  CloseBlob(image);
+  if (image != NULL) {
+    while (image->previous != (Image *) NULL)
+      image=image->previous;
+    CloseBlob(image);
+  }
   return(image);
 }
 
