Index: GraphicsMagick-1.2.5/coders/pcx.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/pcx.c	2017-06-06 12:04:42.681882309 +0200
+++ GraphicsMagick-1.2.5/coders/pcx.c	2017-06-06 12:06:50.084206982 +0200
@@ -189,6 +189,13 @@ static unsigned int IsPCX(const unsigned
 %
 %
 */
+#define ThrowPCXReaderException(code_,reason_,image_) \
+{ \
+  MagickFreeMemory(page_table) \
+  MagickFreeMemory(pcx_pixels); \
+  MagickFreeMemory(scanline); \
+  ThrowReaderException(code_,reason_,image_); \
+}
 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
   Image
@@ -252,7 +259,7 @@ static Image *ReadPCXImage(const ImageIn
   image=AllocateImage(image_info);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == False)
-    ThrowReaderException(FileOpenError,UnableToOpenFile,image);
+    ThrowPCXReaderException(FileOpenError,UnableToOpenFile,image);
   /*
     Determine if this is a PCX file.
   */
@@ -267,11 +274,11 @@ static Image *ReadPCXImage(const ImageIn
       */
       magic=ReadBlobLSBLong(image);
       if (magic != 987654321)
-        ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+        ThrowPCXReaderException(CorruptImageError,ImproperImageHeader,image);
       page_table=MagickAllocateMemory(ExtendedSignedIntegralType *,
          1024*sizeof(ExtendedSignedIntegralType));
       if (page_table == (ExtendedSignedIntegralType *) NULL)
-        ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+        ThrowPCXReaderException(ResourceLimitError,MemoryAllocationFailed,image);
       for (id=0; id < 1024; id++)
       {
         page_table[id]=(ExtendedSignedIntegralType) ReadBlobLSBLong(image);
@@ -282,7 +289,7 @@ static Image *ReadPCXImage(const ImageIn
   if (page_table != (ExtendedSignedIntegralType *) NULL)
     if (SeekBlob(image,(ExtendedSignedIntegralType) page_table[0],SEEK_SET)
         == -1)
-      ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+      ThrowPCXReaderException(CorruptImageError,ImproperImageHeader,image);
   file_size=GetBlobSize(image);
   count=ReadBlob(image,1,(char *) &pcx_info.identifier);
   for (id=1; id < 1024; id++)
@@ -292,7 +299,7 @@ static Image *ReadPCXImage(const ImageIn
     */
     pcx_info.version=ReadBlobByte(image);
     if ((count == 0) || (pcx_info.identifier != 0x0aU))
-      ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+      ThrowPCXReaderException(CorruptImageError,ImproperImageHeader,image);
     pcx_info.encoding=ReadBlobByte(image);
     pcx_info.bits_per_pixel=ReadBlobByte(image);
     pcx_info.left=ReadBlobLSBShort(image);
@@ -309,14 +316,14 @@ static Image *ReadPCXImage(const ImageIn
     image->depth=pcx_info.bits_per_pixel <= 8 ? 8 : QuantumDepth;
     if ((image->columns == 0) || (image->rows == 0) ||
       (pcx_info.bits_per_pixel == 0))
-      ThrowReaderException(CorruptImageError,CorruptImage,image);
+      ThrowPCXReaderException(CorruptImageError,CorruptImage,image);
     image->units=PixelsPerInchResolution;
     image->x_resolution=pcx_info.horizontal_resolution;
     image->y_resolution=pcx_info.vertical_resolution;
     image->colors=16;
     pcx_colormap=MagickAllocateMemory(unsigned char *,3*256);
     if (pcx_colormap == (unsigned char *) NULL)
-      ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+      ThrowPCXReaderException(ResourceLimitError,MemoryAllocationFailed,image);
     (void) ReadBlob(image,3*image->colors,(char *) pcx_colormap);
     pcx_info.reserved=ReadBlobByte(image);
     pcx_info.planes=ReadBlobByte(image);
@@ -329,7 +336,7 @@ static Image *ReadPCXImage(const ImageIn
 	    image->colors = 256;
 	}
     if (!AllocateImageColormap(image,image->colors))
-      ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+      ThrowPCXReaderException(ResourceLimitError,MemoryAllocationFailed,image);
     if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
       image->storage_class=DirectClass;
     p=pcx_colormap;
@@ -361,14 +368,14 @@ static Image *ReadPCXImage(const ImageIn
         {
           /* Not compressed */
           if (uncompressed_size > file_size)
-            ThrowReaderException(CorruptImageError,InsufficientImageDataInFile,
+            ThrowPCXReaderException(CorruptImageError,InsufficientImageDataInFile,
                                    image);
         }
       else
         {
           /* RLE compressed */
           if (uncompressed_size > file_size*254.0)
-            ThrowReaderException(CorruptImageError,InsufficientImageDataInFile,
+            ThrowPCXReaderException(CorruptImageError,InsufficientImageDataInFile,
                                  image);
         }
     }
@@ -382,7 +389,7 @@ static Image *ReadPCXImage(const ImageIn
       (unsigned long) pcx_info.bytes_per_line)*Max(pcx_info.planes,8));
     if ((pcx_pixels == (unsigned char *) NULL) ||
         (scanline == (unsigned char *) NULL))
-      ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+      ThrowPCXReaderException(ResourceLimitError,MemoryAllocationFailed,image);
     if (pcx_info.encoding == 0)
       {
         /*
@@ -393,7 +400,7 @@ static Image *ReadPCXImage(const ImageIn
           {
             packet=ReadBlobByte(image);
             if (EOFBlob(image))
-              ThrowReaderException(CorruptImageError,CorruptImage,image);
+              ThrowPCXReaderException(CorruptImageError,CorruptImage,image);
             *p++=packet;
             pcx_packets--;
             continue;
@@ -409,7 +416,7 @@ static Image *ReadPCXImage(const ImageIn
           {
             packet=ReadBlobByte(image);
             if (EOFBlob(image))
-              ThrowReaderException(CorruptImageError,CorruptImage,image);
+              ThrowPCXReaderException(CorruptImageError,CorruptImage,image);
             if ((packet & 0xc0) != 0xc0)
               {
                 *p++=packet;
@@ -419,7 +426,7 @@ static Image *ReadPCXImage(const ImageIn
             count=packet & 0x3f;
             packet=ReadBlobByte(image);
             if (EOFBlob(image))
-              ThrowReaderException(CorruptImageError,CorruptImage,image);
+              ThrowPCXReaderException(CorruptImageError,CorruptImage,image);
             for (; count != 0; count--)
               {
                 *p++=packet;
@@ -439,7 +446,7 @@ static Image *ReadPCXImage(const ImageIn
             Initialize image colormap.
           */
           if (image->colors > 256)
-            ThrowReaderException(CorruptImageError,ColormapExceeds256Colors,
+            ThrowPCXReaderException(CorruptImageError,ColormapExceeds256Colors,
               image);
           if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)
             {
@@ -643,7 +650,7 @@ static Image *ReadPCXImage(const ImageIn
       break;
     if (SeekBlob(image,(ExtendedSignedIntegralType) page_table[id],SEEK_SET)
         == -1)
-      ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+      ThrowPCXReaderException(CorruptImageError,ImproperImageHeader,image);
     count=ReadBlob(image,1,(char *) &pcx_info.identifier);
     if ((count != 0) && (pcx_info.identifier == 0x0a))
       {
