Index: GraphicsMagick-1.2.5/coders/png.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/png.c	2017-05-05 10:10:20.058596217 +0200
+++ GraphicsMagick-1.2.5/coders/png.c	2017-05-05 10:42:22.198476432 +0200
@@ -2901,15 +2901,35 @@ static Image *ReadOneJNGImage(MngInfo *m
                               type[0],type[1],type[2],type[3],length);
 
       if (length > PNG_MAX_UINT || count == 0)
+      {
+        if (color_image_info != (ImageInfo *)NULL)
+          {
+            DestroyImageInfo(color_image_info);
+          }
+        if (alpha_image_info != (ImageInfo *)NULL)
+          {
+            DestroyImageInfo(alpha_image_info);
+          }
         ThrowReaderException(CorruptImageError,CorruptImage,image);
+      }
       chunk=(unsigned char *) NULL;
       p=NULL;
       if (length)
         {
           chunk=MagickAllocateMemory(unsigned char *,length);
           if (chunk == (unsigned char *) NULL)
+          {
+            if (color_image_info != (ImageInfo *)NULL)
+              {
+                DestroyImageInfo(color_image_info);
+              }
+            if (alpha_image_info != (ImageInfo *)NULL)
+              {
+                DestroyImageInfo(alpha_image_info);
+              }
             ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                  image);
+          }
           for (i=0; i < (long) length; i++)
             chunk[i]=ReadBlobByte(image);
           p=chunk;
@@ -2994,14 +3014,29 @@ static Image *ReadOneJNGImage(MngInfo *m
 
           color_image_info=MagickAllocateMemory(ImageInfo *,sizeof(ImageInfo));
           if (color_image_info == (ImageInfo *) NULL)
+          {
+            if (alpha_image_info != (ImageInfo *)NULL)
+              {
+                DestroyImageInfo(alpha_image_info);
+              }
             ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                  image);
+          }
           GetImageInfo(color_image_info);
           color_image=AllocateImage(color_image_info);
           if (color_image == (Image *) NULL)
+          {
+            if (color_image_info != (ImageInfo *)NULL)
+              {
+                DestroyImageInfo(color_image_info);
+              }
+            if (alpha_image_info != (ImageInfo *)NULL)
+              {
+                DestroyImageInfo(alpha_image_info);
+              }
             ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                  image);
-
+          }
           if (logging)
             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                                   "    Creating color_blob.");
@@ -3009,20 +3044,44 @@ static Image *ReadOneJNGImage(MngInfo *m
           status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode,
                           exception);
           if (status == False)
+          {
+            if (color_image_info != (ImageInfo *)NULL)
+              {
+                DestroyImageInfo(color_image_info);
+              }
+            if (alpha_image_info != (ImageInfo *)NULL)
+              {
+                DestroyImageInfo(alpha_image_info);
+              }
             ThrowReaderException(CoderError,UnableToOpenBlob,color_image);
+          }
 
           if (!image_info->ping && jng_color_type >= 12)
             {
               alpha_image_info=MagickAllocateMemory(ImageInfo *,
                                                     sizeof(ImageInfo));
               if (alpha_image_info == (ImageInfo *) NULL)
+              {
+                if (color_image_info != (ImageInfo *)NULL)
+                  {
+                    DestroyImageInfo(color_image_info);
+                  }
                 ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                      image);
+              }
               GetImageInfo(alpha_image_info);
               alpha_image=AllocateImage(alpha_image_info);
               if (alpha_image == (Image *) NULL)
                 {
-                  DestroyImage(alpha_image);
+                  if (color_image_info != (ImageInfo *)NULL)
+                    {
+                      DestroyImageInfo(color_image_info);
+                    }
+                  if (alpha_image_info != (ImageInfo *)NULL)
+                    {
+                      DestroyImageInfo(alpha_image_info);
+                    }
+
                   ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                        alpha_image);
                 }
@@ -3033,7 +3092,18 @@ static Image *ReadOneJNGImage(MngInfo *m
               status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode,
                               exception);
               if (status == False)
+              {
+                if (color_image_info != (ImageInfo *)NULL)
+                  {
+                    DestroyImageInfo(color_image_info);
+                  }
+                if (alpha_image_info != (ImageInfo *)NULL)
+                  {
+                    DestroyImageInfo(alpha_image_info);
+                  }
+                DestroyImage(alpha_image);
                 ThrowReaderException(CoderError,UnableToOpenBlob,image);
+              }
               if (jng_alpha_compression_method == 0)
                 {
                   unsigned char
@@ -3354,7 +3424,9 @@ static Image *ReadOneJNGImage(MngInfo *m
             }
           (void) LiberateUniqueFileResource(alpha_image->filename);
           DestroyImage(alpha_image);
+          alpha_image = (Image *)NULL;
           DestroyImageInfo(alpha_image_info);
+          alpha_image_info = (ImageInfo *)NULL;
           DestroyImage(jng_image);
         }
     }
@@ -3369,6 +3441,12 @@ static Image *ReadOneJNGImage(MngInfo *m
   mng_info->image_found++;
   (void) MagickMonitor(LoadImagesTag,2*GetBlobSize(image),2*GetBlobSize(image),
                        exception);
+
+  if (alpha_image != (Image *)NULL)
+      DestroyImage(alpha_image);
+  if (alpha_image_info != (ImageInfo *)NULL)
+    DestroyImageInfo(alpha_image_info);
+
   if (logging)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                           "  exit ReadOneJNGImage()");
@@ -3644,7 +3722,10 @@ static Image *ReadMNGImage(const ImageIn
       */
       (void) ReadBlob(image,8,magic_number);
       if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0)
+      {
+        MngInfoFreeStruct(mng_info,&have_mng_structure);
         ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+      }
       /*
         Initialize some nonzero members of the MngInfo structure.
       */
@@ -3701,15 +3782,21 @@ static Image *ReadMNGImage(const ImageIn
           if (length > PNG_MAX_UINT)
             status=False;
           if (count == 0)
+          {
+            MngInfoFreeStruct(mng_info,&have_mng_structure);
             ThrowReaderException(CorruptImageError,CorruptImage,image);
+          }
           p=NULL;
           chunk=(unsigned char *) NULL;
           if (length)
             {
               chunk=MagickAllocateMemory(unsigned char *,length);
               if (chunk == (unsigned char *) NULL)
+              {
+                MngInfoFreeStruct(mng_info,&have_mng_structure);
                 ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                      image);
+              }
               for (i=0; i < (long) length; i++)
                 chunk[i]=ReadBlobByte(image);
               p=chunk;
@@ -3721,8 +3808,11 @@ static Image *ReadMNGImage(const ImageIn
             {
               skip_to_iend=True;
               if (!mng_info->jhdr_warning)
+              {
+                MngInfoFreeStruct(mng_info,&have_mng_structure);
                 (void) ThrowException(&image->exception,CoderError,
                                       JNGCompressionNotSupported,image->filename);
+              }
               mng_info->jhdr_warning++;
             }
 #endif
@@ -3730,8 +3820,11 @@ static Image *ReadMNGImage(const ImageIn
             {
               skip_to_iend=True;
               if (!mng_info->dhdr_warning)
+              {
+                MngInfoFreeStruct(mng_info,&have_mng_structure);
                 (void) ThrowException(&image->exception,CoderError,
                                       DeltaPNGNotSupported,image->filename);
+              }
               mng_info->dhdr_warning++;
             }
           if (!memcmp(type,mng_MEND,4))
@@ -3801,8 +3894,11 @@ static Image *ReadMNGImage(const ImageIn
 
               if ((mng_info->mng_width > 65535L) || (mng_info->mng_height
                                                      > 65535L))
+              {
+                MngInfoFreeStruct(mng_info,&have_mng_structure);
                 (void) ThrowException(&image->exception,ImageError,
                                       WidthOrHeightExceedsLimit,image->filename);
+              }
               FormatString(page_geometry,"%lux%lu+0+0",mng_info->mng_width,
                            mng_info->mng_height);
               mng_info->frame.left=0;
@@ -4618,7 +4714,10 @@ static Image *ReadMNGImage(const ImageIn
             }
 #ifdef MNG_INSERT_LAYERS
           if (length < 8)
+          {
+            MngInfoFreeStruct(mng_info,&have_mng_structure);
             ThrowReaderException(CorruptImageError,ImproperImageHeader,image);
+          }
           image_width=mng_get_long(p);
           image_height=mng_get_long(&p[4]);
 #endif
@@ -4970,7 +5069,7 @@ static Image *ReadMNGImage(const ImageIn
                       MngInfoFreeStruct(mng_info,&have_mng_structure);
                       ThrowReaderException(ResourceLimitError,
                                            MemoryAllocationFailed,image)
-                        }
+                    }
                   n=GetImagePixels(image,0,0,image->columns,1);
                   (void) memcpy(next,n,length);
                   for (y=0; y < (long) image->rows; y++)
@@ -7493,8 +7592,12 @@ static unsigned int WriteOneJNGImage(Mng
                               "  Creating jpeg_image.");
       jpeg_image=CloneImage(image,0,0,True,&image->exception);
       if (jpeg_image == (Image *) NULL)
+      {
+        if (jpeg_image_info != (ImageInfo *)NULL)
+          DestroyImageInfo(jpeg_image_info);
         ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,
                              image);
+      }
       DestroyBlob(jpeg_image);
       jpeg_image->blob=CloneBlobInfo((BlobInfo *) NULL);
       status=ChannelImage(jpeg_image,OpacityChannel);
@@ -7837,8 +7940,12 @@ static unsigned int WriteOneJNGImage(Mng
 
   jpeg_image=CloneImage(image,0,0,True,&image->exception);
   if (jpeg_image == (Image *) NULL)
+  {
+    if (jpeg_image_info != (ImageInfo *)NULL)
+      DestroyImageInfo(jpeg_image_info);
     ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,
                          image);
+  }
   DestroyBlob(jpeg_image);
   jpeg_image->blob=CloneBlobInfo((BlobInfo *) NULL);
   (void) AcquireUniqueFilename(jpeg_image->filename);
