Index: GraphicsMagick-1.2.5/coders/mpc.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/mpc.c	2018-01-05 01:31:27.362326855 +0100
+++ GraphicsMagick-1.2.5/coders/mpc.c	2018-01-05 01:31:27.374327053 +0100
@@ -122,6 +122,21 @@ static unsigned int IsMPC(const unsigned
 %
 %
 */
+#define ThrowMPCReaderException(code_,reason_,image_) \
+do { \
+  if (number_of_profiles > 0) \
+    { \
+      unsigned int _index; \
+      for (_index=0; _index < number_of_profiles; _index++) \
+        { \
+          MagickFreeMemory(profiles[_index].name); \
+          MagickFreeMemory(profiles[_index].info); \
+        } \
+      MagickFreeMemory(profiles); \
+      number_of_profiles=0; \
+    } \
+  ThrowReaderException(code_,reason_,image_); \
+} while (0);
 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
   char
@@ -224,7 +239,7 @@ static Image *ReadMPCImage(const ImageIn
             *p=c;
           }
           if (comment == (char *) NULL)
-            ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
+            ThrowMPCReaderException(ResourceLimitError,MemoryAllocationFailed,
               image);
           *p='\0';
           (void) SetImageAttribute(image,"comment",comment);
@@ -260,7 +275,7 @@ static Image *ReadMPCImage(const ImageIn
                   p=values+strlen(values);
                 }
               if (values == (char *) NULL)
-                ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+                ThrowMPCReaderException(ResourceLimitError,MemoryAllocationFailed,image);
               *p++=c;
               c=ReadBlobByte(image);
               if (*values != '{')
@@ -506,7 +521,7 @@ static Image *ReadMPCImage(const ImageIn
                     i=(long) number_of_profiles;
                     MagickReallocMemory(ProfileInfo *,profiles,(i+1)*sizeof(ProfileInfo));
                     if (profiles == (ProfileInfo *) NULL)
-                      ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+                      ThrowMPCReaderException(ResourceLimitError,MemoryAllocationFailed,image);
                     profiles[i].name=AllocateString(keyword+8);
                     profiles[i].length=atol(values);
                     profiles[i].info=(unsigned char *) NULL;
@@ -648,9 +663,9 @@ static Image *ReadMPCImage(const ImageIn
         (image->storage_class == UndefinedClass) ||
         (image->compression == UndefinedCompression) || (image->columns == 0) ||
         (image->rows == 0))
-      ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+      ThrowMPCReaderException(CorruptImageError,ImproperImageHeader,image);
     if (quantum_depth != QuantumDepth)
-      ThrowReaderException(CacheError,InconsistentPersistentCacheDepth,image);
+      ThrowMPCReaderException(CacheError,InconsistentPersistentCacheDepth,image);
     if (image->montage != (char *) NULL)
       {
         register char
@@ -661,7 +676,7 @@ static Image *ReadMPCImage(const ImageIn
         */
         image->directory=AllocateString((char *) NULL);
         if (image->directory == (char *) NULL)
-          ThrowReaderException(CorruptImageError,UnableToReadImageData,image);
+          ThrowMPCReaderException(CorruptImageError,UnableToReadImageData,image);
         p=image->directory;
         do
         {
@@ -674,7 +689,7 @@ static Image *ReadMPCImage(const ImageIn
               MagickReallocMemory(char *,image->directory,
                 (strlen(image->directory)+MaxTextExtent+1));
               if (image->directory == (char *) NULL)
-                ThrowReaderException(CorruptImageError,UnableToReadImageData,
+                ThrowMPCReaderException(CorruptImageError,UnableToReadImageData,
                   image);
               p=image->directory+strlen(image->directory);
             }
@@ -694,7 +709,7 @@ static Image *ReadMPCImage(const ImageIn
             continue;
           profiles[i].info=MagickAllocateMemory(unsigned char *,profiles[i].length);
           if (profiles[i].info == (unsigned char *) NULL)
-            ThrowReaderException(CorruptImageError,UnableToReadGenericProfile,
+            ThrowMPCReaderException(CorruptImageError,UnableToReadGenericProfile,
               image);
           (void) ReadBlob(image,profiles[i].length,profiles[i].info);
           (void) SetImageProfile(image,profiles[i].name,profiles[i].info,
@@ -712,7 +727,7 @@ static Image *ReadMPCImage(const ImageIn
           Create image colormap.
         */
         if (!AllocateImageColormap(image,image->colors))
-          ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
+          ThrowMPCReaderException(ResourceLimitError,MemoryAllocationFailed,
             image);
         if (image->colors == 0)
           for (i=0; i < 256; i++)
@@ -738,7 +753,7 @@ static Image *ReadMPCImage(const ImageIn
               ThrowReaderException(CorruptImageError,InsufficientImageDataInFile,image);
             colormap=MagickAllocateMemory(unsigned char *,packet_size*image->colors);
             if (colormap == (unsigned char *) NULL)
-              ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
+              ThrowMPCReaderException(ResourceLimitError,MemoryAllocationFailed,
                 image);
             (void) ReadBlob(image,packet_size*image->colors,colormap);
             p=colormap;
