]> granicus.if.org Git - imagemagick/commitdiff
Fixed memory leak reported in #428.
authorDirk Lemstra <dirk@git.imagemagick.org>
Mon, 17 Apr 2017 17:03:27 +0000 (19:03 +0200)
committerDirk Lemstra <dirk@git.imagemagick.org>
Mon, 17 Apr 2017 17:03:27 +0000 (19:03 +0200)
coders/sgi.c

index 3ee90e2a8c2c192e2db24769e8a0ca3bfbff320a..4706ad0d88d48ac02a3e4cecc0911ecdf7b06e8b 100644 (file)
@@ -398,7 +398,10 @@ static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
         scanline=(unsigned char *) AcquireQuantumMemory(iris_info.columns,
           bytes_per_pixel*sizeof(*scanline));
         if (scanline == (unsigned char *) NULL)
-          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          {
+            pixel_info=RelinquishVirtualMemory(pixel_info);
+            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          }
         for (z=0; z < (ssize_t) iris_info.depth; z++)
         {
           p=pixels+bytes_per_pixel*z;
@@ -455,12 +458,11 @@ static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
             (runlength == (size_t *) NULL) ||
             (packet_info == (MemoryInfo *) NULL))
           {
-            if (offsets == (ssize_t *) NULL)
-              offsets=(ssize_t *) RelinquishMagickMemory(offsets);
-            if (runlength == (size_t *) NULL)
-              runlength=(size_t *) RelinquishMagickMemory(runlength);
-            if (packet_info == (MemoryInfo *) NULL)
+            offsets=(ssize_t *) RelinquishMagickMemory(offsets);
+            runlength=(size_t *) RelinquishMagickMemory(runlength);
+            if (packet_info != (MemoryInfo *) NULL)
               packet_info=RelinquishVirtualMemory(packet_info);
+            pixel_info=RelinquishVirtualMemory(pixel_info);
             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
           }
         packets=(unsigned char *) GetVirtualMemoryBlob(packet_info);
@@ -470,7 +472,13 @@ static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
         {
           runlength[i]=ReadBlobMSBLong(image);
           if (runlength[i] > (4*(size_t) iris_info.columns+10))
-            ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+            {
+              packet_info=RelinquishVirtualMemory(packet_info);
+              runlength=(size_t *) RelinquishMagickMemory(runlength);
+              offsets=(ssize_t *) RelinquishMagickMemory(offsets);
+              pixel_info=RelinquishVirtualMemory(pixel_info);
+              ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+            }
         }
         /*
           Check data order.
@@ -507,7 +515,14 @@ static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
                   (ssize_t) iris_info.columns,p+bytes_per_pixel*z);
                 if (status == MagickFalse)
-                  ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+                  {
+                    packet_info=RelinquishVirtualMemory(packet_info);
+                    runlength=(size_t *) RelinquishMagickMemory(runlength);
+                    offsets=(ssize_t *) RelinquishMagickMemory(offsets);
+                    pixel_info=RelinquishVirtualMemory(pixel_info);
+                    ThrowReaderException(CorruptImageError,
+                      "ImproperImageHeader");
+                  }
                 p+=(iris_info.columns*4*bytes_per_pixel);
               }
             }
@@ -538,7 +553,14 @@ static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
                   (ssize_t) iris_info.columns,p+bytes_per_pixel*z);
                 if (status == MagickFalse)
-                  ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+                  {
+                    packet_info=RelinquishVirtualMemory(packet_info);
+                    runlength=(size_t *) RelinquishMagickMemory(runlength);
+                    offsets=(ssize_t *) RelinquishMagickMemory(offsets);
+                    pixel_info=RelinquishVirtualMemory(pixel_info);
+                    ThrowReaderException(CorruptImageError,
+                      "ImproperImageHeader");
+                  }
               }
               p+=(iris_info.columns*4*bytes_per_pixel);
             }