Index: GraphicsMagick-1.2.5/coders/png.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/png.c	2018-01-17 07:13:06.347290692 +0100
+++ GraphicsMagick-1.2.5/coders/png.c	2018-01-17 07:48:51.646598465 +0100
@@ -2753,6 +2753,21 @@ static Image *ReadPNGImage(const ImageIn
 
 
 #if defined(JNG_SUPPORTED)
+void
+DestroyJNG(unsigned char *chunk,Image *color_image,ImageInfo *color_image_info,
+   Image *alpha_image,ImageInfo *alpha_image_info)
+{
+  if (chunk)
+    MagickFreeMemory(chunk);
+  if (color_image_info)
+    DestroyImageInfo(color_image_info);
+  if (alpha_image_info)
+    DestroyImageInfo(alpha_image_info);
+  if (color_image)
+    DestroyImage(color_image);
+  if (alpha_image)
+    DestroyImage(alpha_image);
+}
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
@@ -2916,14 +2931,8 @@ static Image *ReadOneJNGImage(MngInfo *m
 
       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);
-          }
+        DestroyJNG(NULL,color_image,color_image_info,
+                   alpha_image,alpha_image_info);
         ThrowReaderException(CorruptImageError,CorruptImage,image);
       }
       chunk=(unsigned char *) NULL;
@@ -2933,14 +2942,8 @@ static Image *ReadOneJNGImage(MngInfo *m
           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);
-              }
+            DestroyJNG(chunk,color_image,color_image_info,
+                       alpha_image,alpha_image_info);
             ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                  image);
           }
@@ -3022,15 +3025,8 @@ static Image *ReadOneJNGImage(MngInfo *m
                 {
                   ThrowException(exception,CorruptImageError,
                                  InsufficientImageDataInFile,image->filename);
-                  if (color_image_info != (ImageInfo *)NULL)
-                    {
-                      DestroyImageInfo(color_image_info);
-                    }
-                  if (alpha_image_info != (ImageInfo *)NULL)
-                    {
-                      DestroyImageInfo(alpha_image_info);
-                    }
-                  MagickFreeMemory(chunk);
+                  DestroyJNG(chunk,color_image,color_image_info,
+                             alpha_image,alpha_image_info);
                   return ((Image *)NULL);
                 }
             }
@@ -3054,10 +3050,8 @@ 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);
-              }
+            DestroyJNG(chunk,color_image,color_image_info,
+                       alpha_image,alpha_image_info);
             ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                  image);
           }
@@ -3065,14 +3059,8 @@ static Image *ReadOneJNGImage(MngInfo *m
           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);
-              }
+            DestroyJNG(chunk,color_image,color_image_info,
+                       alpha_image,alpha_image_info);
             ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                  image);
           }
@@ -3084,14 +3072,8 @@ static Image *ReadOneJNGImage(MngInfo *m
                           exception);
           if (status == False)
           {
-            if (color_image_info != (ImageInfo *)NULL)
-              {
-                DestroyImageInfo(color_image_info);
-              }
-            if (alpha_image_info != (ImageInfo *)NULL)
-              {
-                DestroyImageInfo(alpha_image_info);
-              }
+            DestroyJNG(chunk,color_image,color_image_info,
+                       alpha_image,alpha_image_info);
             ThrowReaderException(CoderError,UnableToOpenBlob,color_image);
           }
 
@@ -3101,10 +3083,8 @@ static Image *ReadOneJNGImage(MngInfo *m
                                                     sizeof(ImageInfo));
               if (alpha_image_info == (ImageInfo *) NULL)
               {
-                if (color_image_info != (ImageInfo *)NULL)
-                  {
-                    DestroyImageInfo(color_image_info);
-                  }
+                DestroyJNG(chunk,color_image,color_image_info,
+                           alpha_image,alpha_image_info);
                 ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                      image);
               }
@@ -3112,15 +3092,8 @@ static Image *ReadOneJNGImage(MngInfo *m
               alpha_image=AllocateImage(alpha_image_info);
               if (alpha_image == (Image *) NULL)
                 {
-                  if (color_image_info != (ImageInfo *)NULL)
-                    {
-                      DestroyImageInfo(color_image_info);
-                    }
-                  if (alpha_image_info != (ImageInfo *)NULL)
-                    {
-                      DestroyImageInfo(alpha_image_info);
-                    }
-
+                  DestroyJNG(chunk,color_image,color_image_info,
+                             alpha_image,alpha_image_info);
                   ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,
                                        alpha_image);
                 }
@@ -3132,14 +3105,8 @@ static Image *ReadOneJNGImage(MngInfo *m
                               exception);
               if (status == False)
               {
-                if (color_image_info != (ImageInfo *)NULL)
-                  {
-                    DestroyImageInfo(color_image_info);
-                  }
-                if (alpha_image_info != (ImageInfo *)NULL)
-                  {
-                    DestroyImageInfo(alpha_image_info);
-                  }
+                DestroyJNG(chunk,color_image,color_image_info,
+                           alpha_image,alpha_image_info);
                 DestroyImage(alpha_image);
                 ThrowReaderException(CoderError,UnableToOpenBlob,image);
               }
@@ -3357,7 +3324,7 @@ static Image *ReadOneJNGImage(MngInfo *m
     }
 
 
-  /* IEND found */
+  /* IEND found or loop ended */
 
   /*
     Finish up reading image data:
@@ -3391,7 +3358,9 @@ static Image *ReadOneJNGImage(MngInfo *m
   jng_image=ReadImage(color_image_info,exception);
   (void) LiberateUniqueFileResource(color_image->filename);
   DestroyImage(color_image);
+  color_image = (Image *)NULL;
   DestroyImageInfo(color_image_info);
+  color_image_info = (ImageInfo *)NULL;
 
   if (jng_image == (Image *) NULL)
     {
@@ -3479,11 +3448,10 @@ static Image *ReadOneJNGImage(MngInfo *m
                 break;
             }
           (void) LiberateUniqueFileResource(alpha_image->filename);
-          DestroyImage(alpha_image);
-          alpha_image = (Image *)NULL;
-          DestroyImageInfo(alpha_image_info);
-          alpha_image_info = (ImageInfo *)NULL;
+          DestroyJNG(NULL,color_image,color_image_info,
+                     alpha_image,alpha_image_info);
           DestroyImage(jng_image);
+          jng_image = (Image *)NULL;
         }
     }
 
@@ -3498,10 +3466,8 @@ static Image *ReadOneJNGImage(MngInfo *m
   (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);
+  DestroyJNG(NULL,color_image,color_image_info,
+             alpha_image,alpha_image_info);
 
   if (logging)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
