Commit 85154e193 for imagemagick.org

commit 85154e193c63f146e80b587b9f2707cb6bc54980
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sun Nov 29 09:25:35 2026 -0500

    all error paths now free the buffers and call CloseBlob()

diff --git a/coders/ept.c b/coders/ept.c
index 22dd1acab..ee4f5616b 100644
--- a/coders/ept.c
+++ b/coders/ept.c
@@ -93,6 +93,15 @@ typedef struct _EPTInfo
 */
 static MagickBooleanType
   WriteEPTImage(const ImageInfo *,Image *,ExceptionInfo *);
+
+static void DestroyEPTInfo(EPTInfo *ept_info)
+{
+  if (ept_info->tiff != (unsigned char *) NULL)
+    ept_info->tiff=(unsigned char *) RelinquishMagickMemory(ept_info->tiff);
+  if (ept_info->postscript != (unsigned char *) NULL)
+    ept_info->postscript=(unsigned char *) RelinquishMagickMemory(
+      ept_info->postscript);
+}

 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -157,10 +166,6 @@ static MagickBooleanType IsEPT(const unsigned char *magick,const size_t length)
 */
 static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
-  const void
-    *postscript_data,
-    *tiff_data;
-
   EPTInfo
     ept_info;

@@ -177,6 +182,9 @@ static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
   MagickOffsetType
     offset;

+  MagickSizeType
+    blob_size;
+
   ssize_t
     count;

@@ -197,70 +205,111 @@ static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image=DestroyImageList(image);
       return((Image *) NULL);
     }
+  (void) memset(&ept_info,0,sizeof(ept_info));
   ept_info.magick=ReadBlobLSBLong(image);
   if (ept_info.magick != 0xc6d3d0c5ul)
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   ept_info.postscript_offset=(MagickOffsetType) ReadBlobLSBLong(image);
   ept_info.postscript_length=ReadBlobLSBLong(image);
-  if (HeapOverflowCheckAdd(ept_info.postscript_length,1) != MagickFalse)
-    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-  if ((MagickSizeType) ept_info.postscript_length > GetBlobSize(image))
-    ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
   (void) ReadBlobLSBLong(image);
   (void) ReadBlobLSBLong(image);
   ept_info.tiff_offset=(MagickOffsetType) ReadBlobLSBLong(image);
   ept_info.tiff_length=ReadBlobLSBLong(image);
-  if (HeapOverflowCheckAdd(ept_info.tiff_length,1) != MagickFalse)
-    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-  if ((ept_info.postscript_length+ept_info.tiff_length) == 0)
-    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-  if ((MagickSizeType) ept_info.tiff_length > GetBlobSize(image))
-    ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
   (void) ReadBlobLSBShort(image);
-  ept_info.postscript=(unsigned char *) AcquireQuantumMemory(
-    ept_info.postscript_length+1,sizeof(*ept_info.postscript));
-  if (ept_info.postscript == (unsigned char *) NULL)
+  if (EOFBlob(image) != MagickFalse)
+    ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
+  if ((ept_info.postscript_length == 0) && (ept_info.tiff_length == 0))
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  if ((ept_info.postscript_length == ~((size_t) 0)) ||
+      (ept_info.tiff_length == ~((size_t) 0)))
     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-  (void) memset(ept_info.postscript,0,(ept_info.postscript_length+1)*
-    sizeof(*ept_info.postscript));
-  ept_info.tiff=(unsigned char *) AcquireQuantumMemory(ept_info.tiff_length+1,
-    sizeof(*ept_info.tiff));
-  if (ept_info.tiff == (unsigned char *) NULL)
+  /*
+    Validate that each section lies entirely within the file.
+  */
+  blob_size=GetBlobSize(image);
+  if (ept_info.postscript_length != 0)
+    {
+      if (ept_info.postscript_offset < 30)
+        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+      if (((MagickSizeType) ept_info.postscript_offset+
+           ept_info.postscript_length) > blob_size)
+        ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
+    }
+  if (ept_info.tiff_length != 0)
     {
-      ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
-        ept_info.postscript);
-      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      if (ept_info.tiff_offset < 30)
+        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+      if (((MagickSizeType) ept_info.tiff_offset+ept_info.tiff_length) >
+          blob_size)
+        ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
     }
-  (void) memset(ept_info.tiff,0,(ept_info.tiff_length+1)*
-    sizeof(*ept_info.tiff));
-  offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET);
-  if ((ept_info.tiff_length != 0) && (offset < 30))
+  if (ept_info.postscript_length != 0)
     {
-      ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
-      ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
-        ept_info.postscript);
-      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+      ept_info.postscript=(unsigned char *) AcquireQuantumMemory(
+        ept_info.postscript_length+1,sizeof(*ept_info.postscript));
+      if (ept_info.postscript == (unsigned char *) NULL)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      (void) memset(ept_info.postscript,0,(ept_info.postscript_length+1)*
+        sizeof(*ept_info.postscript));
     }
-  tiff_data=ReadBlobStream(image,ept_info.tiff_length,ept_info.tiff,&count);
-  if (count != (ssize_t) (ept_info.tiff_length))
-    (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageWarning,
-      "InsufficientImageDataInFile","`%s'",image->filename);
-  offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET);
-  if ((ept_info.postscript_length != 0) && (offset < 30))
+  if (ept_info.tiff_length != 0)
+    {
+      ept_info.tiff=(unsigned char *) AcquireQuantumMemory(
+        ept_info.tiff_length+1,sizeof(*ept_info.tiff));
+      if (ept_info.tiff == (unsigned char *) NULL)
+        {
+          DestroyEPTInfo(&ept_info);
+          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+        }
+      (void) memset(ept_info.tiff,0,(ept_info.tiff_length+1)*
+        sizeof(*ept_info.tiff));
+    }
+  /*
+    Read both sections into private buffers (ReadBlob, not ReadBlobStream: the
+    latter may hand back a pointer into the blob, which is destroyed below).
+  */
+  if (ept_info.tiff_length != 0)
     {
-      ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
-      ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
-        ept_info.postscript);
-      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+      offset=SeekBlob(image,ept_info.tiff_offset,SEEK_SET);
+      if (offset != ept_info.tiff_offset)
+        {
+          DestroyEPTInfo(&ept_info);
+          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+        }
+      count=ReadBlob(image,ept_info.tiff_length,ept_info.tiff);
+      if (count != (ssize_t) ept_info.tiff_length)
+        {
+          if (ept_info.postscript_length == 0)
+            {
+              DestroyEPTInfo(&ept_info);
+              ThrowReaderException(CorruptImageError,
+                "InsufficientImageDataInFile");
+            }
+          /*
+            Truncated preview: warn and carry on with the PostScript only.
+          */
+          (void) ThrowMagickException(exception,GetMagickModule(),
+            CorruptImageWarning,"InsufficientImageDataInFile","`%s'",
+            image->filename);
+          ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
+          ept_info.tiff_length=0;
+        }
     }
-  postscript_data=ReadBlobStream(image,ept_info.postscript_length,
-    ept_info.postscript,&count);
-  if (count != (ssize_t) (ept_info.postscript_length))
+  if (ept_info.postscript_length != 0)
     {
-      ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
-      ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
-        ept_info.postscript);
-      ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
+      offset=SeekBlob(image,ept_info.postscript_offset,SEEK_SET);
+      if (offset != ept_info.postscript_offset)
+        {
+          DestroyEPTInfo(&ept_info);
+          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+        }
+      count=ReadBlob(image,ept_info.postscript_length,ept_info.postscript);
+      if (count != (ssize_t) ept_info.postscript_length)
+        {
+          DestroyEPTInfo(&ept_info);
+          ThrowReaderException(CorruptImageError,
+            "InsufficientImageDataInFile");
+        }
     }
   (void) CloseBlob(image);
   image=DestroyImage(image);
@@ -273,8 +322,8 @@ static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
       /*
         Convert Postscript blob to image.
       */
-      image=BlobToImage(read_info,postscript_data,ept_info.postscript_length,
-        exception);
+      image=BlobToImage(read_info,ept_info.postscript,
+        ept_info.postscript_length,exception);
       if (image != (Image *) NULL)
         {
           (void) CopyMagickString(image->filename,image_info->filename,
@@ -288,7 +337,7 @@ static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
         Convert TIFF blob to image.
       */
       (void) CopyMagickString(read_info->magick,"TIFF",MagickPathExtent);
-      tiff_image=BlobToImage(read_info,tiff_data,ept_info.tiff_length,
+      tiff_image=BlobToImage(read_info,ept_info.tiff,ept_info.tiff_length,
         exception);
       if (tiff_image != (Image *) NULL)
         {
@@ -299,9 +348,7 @@ static Image *ReadEPTImage(const ImageInfo *image_info,ExceptionInfo *exception)
         }
     }
   read_info=DestroyImageInfo(read_info);
-  ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
-  ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
-    ept_info.postscript);
+  DestroyEPTInfo(&ept_info);
   return(GetFirstImageInList(image));
 }

@@ -339,17 +386,17 @@ ModuleExport size_t RegisterEPTImage(void)
   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
   entry->magick=(IsImageFormatHandler *) IsEPT;
   entry->flags|=CoderDecoderSeekableStreamFlag;
-  entry->flags^=CoderAdjoinFlag;
-  entry->flags^=CoderBlobSupportFlag;
+  entry->flags&=~CoderAdjoinFlag;
+  entry->flags&=~CoderBlobSupportFlag;
   (void) RegisterMagickInfo(entry);
   entry=AcquireMagickInfo("EPT","EPT2",
     "Encapsulated PostScript Level II with TIFF preview");
   entry->decoder=(DecodeImageHandler *) ReadEPTImage;
   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
   entry->magick=(IsImageFormatHandler *) IsEPT;
-  entry->flags^=CoderAdjoinFlag;
   entry->flags|=CoderDecoderSeekableStreamFlag;
-  entry->flags^=CoderBlobSupportFlag;
+  entry->flags&=~CoderAdjoinFlag;
+  entry->flags&=~CoderBlobSupportFlag;
   (void) RegisterMagickInfo(entry);
   entry=AcquireMagickInfo("EPT","EPT3",
     "Encapsulated PostScript Level III with TIFF preview");
@@ -357,7 +404,8 @@ ModuleExport size_t RegisterEPTImage(void)
   entry->encoder=(EncodeImageHandler *) WriteEPTImage;
   entry->magick=(IsImageFormatHandler *) IsEPT;
   entry->flags|=CoderDecoderSeekableStreamFlag;
-  entry->flags^=CoderBlobSupportFlag;
+  entry->flags&=~CoderAdjoinFlag;
+  entry->flags&=~CoderBlobSupportFlag;
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -383,6 +431,8 @@ ModuleExport size_t RegisterEPTImage(void)
 */
 ModuleExport void UnregisterEPTImage(void)
 {
+  (void) UnregisterMagickInfo("EPT3");
+  (void) UnregisterMagickInfo("EPT2");
   (void) UnregisterMagickInfo("EPT");
 }

@@ -432,6 +482,9 @@ static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
   MagickBooleanType
     status;

+  ssize_t
+    count;
+
   /*
     Write EPT image.
   */
@@ -446,9 +499,14 @@ static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
+  (void) memset(&ept_info,0,sizeof(ept_info));
+  ept_info.magick=0xc6d3d0c5ul;
   write_image=CloneImage(image,0,0,MagickTrue,exception);
   if (write_image == (Image *) NULL)
-    return(MagickFalse);
+    {
+      (void) CloseBlob(image);
+      return(MagickFalse);
+    }
   write_info=CloneImageInfo(image_info);
   (void) CopyMagickString(write_info->filename,"EPS:",MagickPathExtent);
   (void) CopyMagickString(write_info->magick,"EPS",MagickPathExtent);
@@ -462,17 +520,22 @@ static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
       (void) CopyMagickString(write_info->filename,"EPS3:",MagickPathExtent);
       (void) CopyMagickString(write_info->magick,"EPS3",MagickPathExtent);
     }
-  (void) memset(&ept_info,0,sizeof(ept_info));
-  ept_info.magick=0xc6d3d0c5ul;
   ept_info.postscript=(unsigned char *) ImageToBlob(write_info,write_image,
     &ept_info.postscript_length,exception);
   write_image=DestroyImage(write_image);
   write_info=DestroyImageInfo(write_info);
-  if (ept_info.postscript == (void *) NULL)
-    return(MagickFalse);
+  if (ept_info.postscript == (unsigned char *) NULL)
+    {
+      (void) CloseBlob(image);
+      return(MagickFalse);
+    }
   write_image=CloneImage(image,0,0,MagickTrue,exception);
   if (write_image == (Image *) NULL)
-    return(MagickFalse);
+    {
+      DestroyEPTInfo(&ept_info);
+      (void) CloseBlob(image);
+      return(MagickFalse);
+    }
   write_info=CloneImageInfo(image_info);
   (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
   (void) FormatLocaleString(filename,MagickPathExtent,"tiff:%s",
@@ -480,10 +543,23 @@ static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
   (void) CopyMagickString(write_info->filename,filename,MagickPathExtent);
   if ((write_image->columns > 512) || (write_image->rows > 512))
     {
+      double
+        scale;
+
       Image
         *resize_image;

-      resize_image=ResizeImage(write_image,512,512,write_image->filter,
+      size_t
+        columns,
+        rows;
+
+      /*
+        Shrink the preview to fit within 512x512, preserving aspect ratio.
+      */
+      scale=512.0/(double) MagickMax(write_image->columns,write_image->rows);
+      columns=MagickMax((size_t) (scale*write_image->columns+0.5),1);
+      rows=MagickMax((size_t) (scale*write_image->rows+0.5),1);
+      resize_image=ResizeImage(write_image,columns,rows,write_image->filter,
         exception);
       if (resize_image != (Image *) NULL)
         {
@@ -501,19 +577,39 @@ static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
         EPT preview requires that the image is colormapped.
       */
       GetQuantizeInfo(&quantize_info);
+      quantize_info.number_colors=256;
       quantize_info.dither_method=IdentifyPaletteImage(write_image,
         exception) == MagickFalse ? RiemersmaDitherMethod : NoDitherMethod;
-      (void) QuantizeImage(&quantize_info,write_image,exception);
+      if (QuantizeImage(&quantize_info,write_image,exception) == MagickFalse)
+        {
+          write_image=DestroyImage(write_image);
+          write_info=DestroyImageInfo(write_info);
+          DestroyEPTInfo(&ept_info);
+          (void) CloseBlob(image);
+          return(MagickFalse);
+        }
     }
   write_info->compression=NoCompression;
   ept_info.tiff=(unsigned char *) ImageToBlob(write_info,write_image,
     &ept_info.tiff_length,exception);
   write_image=DestroyImage(write_image);
   write_info=DestroyImageInfo(write_info);
-  if (ept_info.tiff == (void *) NULL)
+  if (ept_info.tiff == (unsigned char *) NULL)
     {
-      ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
-        ept_info.postscript);
+      DestroyEPTInfo(&ept_info);
+      (void) CloseBlob(image);
+      return(MagickFalse);
+    }
+  /*
+    The header stores 32-bit offsets and lengths.
+  */
+  if (((MagickSizeType) ept_info.postscript_length+ept_info.tiff_length+30) >
+      (MagickSizeType) 0xffffffffUL)
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),
+        ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+      DestroyEPTInfo(&ept_info);
+      (void) CloseBlob(image);
       return(MagickFalse);
     }
   /*
@@ -527,14 +623,16 @@ static MagickBooleanType WriteEPTImage(const ImageInfo *image_info,Image *image,
   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.postscript_length+30);
   (void) WriteBlobLSBLong(image,(unsigned int) ept_info.tiff_length);
   (void) WriteBlobLSBShort(image,0xffff);
-  (void) WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
-  (void) WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
+  count=WriteBlob(image,ept_info.postscript_length,ept_info.postscript);
+  if (count != (ssize_t) ept_info.postscript_length)
+    status=MagickFalse;
+  count=WriteBlob(image,ept_info.tiff_length,ept_info.tiff);
+  if (count != (ssize_t) ept_info.tiff_length)
+    status=MagickFalse;
   /*
     Relinquish resources.
   */
-  ept_info.postscript=(unsigned char *) RelinquishMagickMemory(
-    ept_info.postscript);
-  ept_info.tiff=(unsigned char *) RelinquishMagickMemory(ept_info.tiff);
+  DestroyEPTInfo(&ept_info);
   if (CloseBlob(image) == MagickFalse)
     status=MagickFalse;
   return(status);