]> granicus.if.org Git - imagemagick/commitdiff
Improve exception handling
authorCristy <urban-warrior@imagemagick.org>
Sat, 17 Feb 2018 17:48:13 +0000 (12:48 -0500)
committerCristy <urban-warrior@imagemagick.org>
Sat, 17 Feb 2018 17:48:13 +0000 (12:48 -0500)
coders/gif.c

index cb30755b387d3f04ddf7c6dced110fbbd9409590..a067a717a66d19deb5b504b5bdca22ad79de3f28 100644 (file)
@@ -954,6 +954,16 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
 #define BitSet(byte,bit)  (((byte) & (bit)) == (bit))
 #define LSBFirstOrder(x,y)  (((y) << 8) | (x))
+#define ThrowGIFException(exception,message) \
+{ \
+  if (profiles != (LinkedListInfo *) NULL) \
+    profiles=DestroyLinkedList(profiles,DestroyGIFProfile); \
+  if (global_colormap != (unsigned char *) NULL) \
+    global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap); \
+  if (meta_image != (Image *) NULL) \
+    meta_image=DestroyImage(meta_image); \
+  ThrowReaderException((exception),(message)); \
+}
 
   Image
     *image,
@@ -965,9 +975,6 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
   MagickBooleanType
     status;
 
-  MagickSizeType
-    max_image_count;
-
   register ssize_t
     i;
 
@@ -1049,7 +1056,6 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
   duration=0;
   opacity=(-1);
   image_count=0;
-  max_image_count=GetMagickResourceLimit(ListLengthResource);
   for ( ; ; )
   {
     count=ReadBlob(image,1,&c);
@@ -1064,15 +1070,7 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
         */
         count=ReadBlob(image,1,&c);
         if (count != 1)
-          {
-            if (profiles != (LinkedListInfo *) NULL)
-              profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-            global_colormap=(unsigned char *) RelinquishMagickMemory(
-              global_colormap);
-            meta_image=DestroyImage(meta_image);
-            ThrowReaderException(CorruptImageError,
-              "UnableToReadExtensionBlock");
-          }
+          ThrowGIFException(CorruptImageError,"UnableToReadExtensionBlock");
         switch (c)
         {
           case 0xf9:
@@ -1168,15 +1166,8 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 info=(unsigned char *) AcquireQuantumMemory(255UL,
                   sizeof(*info));
                 if (info == (unsigned char *) NULL)
-                  {
-                    if (profiles != (LinkedListInfo *) NULL)
-                      profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-                    meta_image=DestroyImage(meta_image);
-                    global_colormap=(unsigned char *) RelinquishMagickMemory(
-                      global_colormap);
-                    ThrowReaderException(ResourceLimitError,
-                      "MemoryAllocationFailed");
-                  }
+                  ThrowGIFException(ResourceLimitError,
+                    "MemoryAllocationFailed");
                 (void) ResetMagickMemory(info,0,255UL*sizeof(*info));
                 reserved_length=255;
                 for (info_length=0; ; )
@@ -1192,9 +1183,7 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
                         reserved_length,sizeof(*info));
                       if (info == (unsigned char *) NULL)
                         {
-                          meta_image=DestroyImage(meta_image);
-                          global_colormap=(unsigned char *)
-                            RelinquishMagickMemory(global_colormap);
+                          info=(unsigned char *) RelinquishMagickMemory(info);
                           ThrowReaderException(ResourceLimitError,
                             "MemoryAllocationFailed");
                         }
@@ -1203,11 +1192,6 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 profile=BlobToStringInfo(info,(size_t) info_length);
                 if (profile == (StringInfo *) NULL)
                   {
-                    if (profiles != (LinkedListInfo *) NULL)
-                      profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-                    meta_image=DestroyImage(meta_image);
-                    global_colormap=(unsigned char *) RelinquishMagickMemory(
-                      global_colormap);
                     info=(unsigned char *) RelinquishMagickMemory(info);
                     ThrowReaderException(ResourceLimitError,
                       "MemoryAllocationFailed");
@@ -1254,15 +1238,6 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
     image_count++;
     if (image_count != 1)
       {
-        if ((MagickSizeType) image_count == max_image_count)
-          {
-            if (profiles != (LinkedListInfo *) NULL)
-              profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-            image=DestroyImageList(image);
-            global_colormap=(unsigned char *) RelinquishMagickMemory(
-              global_colormap);
-            ThrowReaderException(CorruptImageError,"ListLengthExceedsLimit");
-          }
         /*
           Allocate next image structure.
         */
@@ -1320,26 +1295,12 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
     image->ticks_per_second=100;
     image->alpha_trait=opacity >= 0 ? BlendPixelTrait : UndefinedPixelTrait;
     if ((image->columns == 0) || (image->rows == 0))
-      {
-        if (profiles != (LinkedListInfo *) NULL)
-          profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-        global_colormap=(unsigned char *) RelinquishMagickMemory(
-          global_colormap);
-        meta_image=DestroyImage(meta_image);
-        ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
-      }
+      ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
     /*
       Inititialize colormap.
     */
     if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
-      {
-        if (profiles != (LinkedListInfo *) NULL)
-          profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-        global_colormap=(unsigned char *) RelinquishMagickMemory(
-          global_colormap);
-        meta_image=DestroyImage(meta_image);
-        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-      }
+      ThrowGIFException(ResourceLimitError,"MemoryAllocationFailed");
     if (BitSet((int) flag,0x80) == 0)
       {
         /*
@@ -1371,27 +1332,14 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
         colormap=(unsigned char *) AcquireQuantumMemory((size_t)
           MagickMax(local_colors,256),3UL*sizeof(*colormap));
         if (colormap == (unsigned char *) NULL)
-          {
-            if (profiles != (LinkedListInfo *) NULL)
-              profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-            global_colormap=(unsigned char *) RelinquishMagickMemory(
-              global_colormap);
-            meta_image=DestroyImage(meta_image);
-            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-          }
+          ThrowGIFException(ResourceLimitError,"MemoryAllocationFailed");
         (void) ResetMagickMemory(colormap,0,3*MagickMax(local_colors,256)*
           sizeof(*colormap));
         count=ReadBlob(image,(3*local_colors)*sizeof(*colormap),colormap);
         if (count != (ssize_t) (3*local_colors))
           {
-            if (profiles != (LinkedListInfo *) NULL)
-              profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-            global_colormap=(unsigned char *) RelinquishMagickMemory(
-              global_colormap);
             colormap=(unsigned char *) RelinquishMagickMemory(colormap);
-            meta_image=DestroyImage(meta_image);
-            ThrowReaderException(CorruptImageError,
-              "InsufficientImageDataInFile");
+            ThrowGIFException(CorruptImageError,"InsufficientImageDataInFile");
           }
         p=colormap;
         for (i=0; i < (ssize_t) image->colors; i++)
@@ -1433,21 +1381,14 @@ static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
     else
       status=DecodeImage(image,opacity,exception);
     if ((image_info->ping == MagickFalse) && (status == MagickFalse))
-      {
-        if (profiles != (LinkedListInfo *) NULL)
-          profiles=DestroyLinkedList(profiles,DestroyGIFProfile);
-        global_colormap=(unsigned char *) RelinquishMagickMemory(
-          global_colormap);
-        meta_image=DestroyImage(meta_image);
-        ThrowReaderException(CorruptImageError,"CorruptImage");
-      }
+      ThrowGIFException(CorruptImageError,"CorruptImage");
     duration+=image->delay*image->iterations;
     if (image_info->number_scenes != 0)
       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
         break;
     opacity=(-1);
-    status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) image->scene-
-      1,image->scene);
+    status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+      image->scene-1,image->scene);
     if (status == MagickFalse)
       break;
   }