Commit e5c3e64fb for imagemagick.org

commit e5c3e64fba02dabe6aa134b34c440567c3ed8e1d
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Wed Dec 2 17:39:54 2026 -0500

    stop a tiny file with huge dimensions from allocating a large image

diff --git a/coders/flif.c b/coders/flif.c
index 613dd8e04..78a442dd6 100644
--- a/coders/flif.c
+++ b/coders/flif.c
@@ -632,7 +632,7 @@ static MagickBooleanType WriteFLIFImage(const ImageInfo *image_info,
       count=(size_t) WriteBlob(image,length,(const unsigned char *) buffer);
       if (count != length)
         {
-          (void) ThrowMagickException(exception,GetMagickModule(),WriteError,
+          (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
             "UnableToWriteBlob","`%s'",image->filename);
           status=MagickFalse;
         }
diff --git a/coders/mono.c b/coders/mono.c
index e57565523..2743e58a7 100644
--- a/coders/mono.c
+++ b/coders/mono.c
@@ -80,7 +80,7 @@ static MagickBooleanType
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  ReadMONOImage() reads an image of raw bites in LSB order and returns
+%  ReadMONOImage() reads an image of raw bits in LSB order and returns
 %  it.  It allocates the memory necessary for the new Image structure and
 %  returns a pointer to the new image.
 %
@@ -102,20 +102,25 @@ static Image *ReadMONOImage(const ImageInfo *image_info,
   Image
     *image;

+  int
+    c;
+
   MagickBooleanType
     status;

+  MagickSizeType
+    blob_size,
+    length;
+
   Quantum
     *q;

-  ssize_t
-    x;
-
   size_t
     bit,
     byte;

   ssize_t
+    x,
     y;

   /*
@@ -137,9 +142,10 @@ static Image *ReadMONOImage(const ImageInfo *image_info,
       image=DestroyImageList(image);
       return((Image *) NULL);
     }
+  if (image->offset < 0)
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   if (DiscardBlobBytes(image,(MagickSizeType) image->offset) == MagickFalse)
-    ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
-      image->filename);
+    ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
   /*
     Initialize image colormap.
   */
@@ -154,6 +160,16 @@ static Image *ReadMONOImage(const ImageInfo *image_info,
   status=SetImageExtent(image,image->columns,image->rows,exception);
   if (status == MagickFalse)
     return(DestroyImageList(image));
+  /*
+    Reject files that are too small for the requested dimensions before
+    committing to the pixel loop (only when the blob size is known).
+  */
+  length=(MagickSizeType) ((image->columns+7)/8);
+  blob_size=GetBlobSize(image);
+  if ((blob_size != 0) && ((MagickSizeType) image->offset <= blob_size) &&
+      ((MagickSizeType) image->rows > ((blob_size-(MagickSizeType)
+        image->offset)/length)))
+    ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
   /*
     Convert bi-level image to pixel packets.
   */
@@ -161,14 +177,22 @@ static Image *ReadMONOImage(const ImageInfo *image_info,
   {
     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
     if (q == (Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     bit=0;
     byte=0;
     for (x=0; x < (ssize_t) image->columns; x++)
     {
       if (bit == 0)
-        byte=(size_t) ReadBlobByte(image);
-      if (image_info->endian == LSBEndian)
+        {
+          c=ReadBlobByte(image);
+          if (c == EOF)
+            break;
+          byte=(size_t) c;
+        }
+      if (image->endian == LSBEndian)
         SetPixelIndex(image,(Quantum) (((byte & 0x01) != 0) ? 0x00 : 0x01),q);
       else
         SetPixelIndex(image,(Quantum) (((byte & 0x01) != 0) ? 0x01 : 0x00),q);
@@ -179,7 +203,12 @@ static Image *ReadMONOImage(const ImageInfo *image_info,
       q+=(ptrdiff_t) GetPixelChannels(image);
     }
     if (SyncAuthenticPixels(image,exception) == MagickFalse)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
+    if (x < (ssize_t) image->columns)
+      break;  /* premature end of file; reported below */
     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
       image->rows);
     if (status == MagickFalse)
@@ -229,7 +258,7 @@ ModuleExport size_t RegisterMONOImage(void)
   entry->encoder=(EncodeImageHandler *) WriteMONOImage;
   entry->flags|=CoderRawSupportFlag;
   entry->flags|=CoderEndianSupportFlag;
-  entry->flags^=CoderAdjoinFlag;
+  entry->flags&=~CoderAdjoinFlag;
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -328,8 +357,13 @@ static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
   /*
     Convert image to a bi-level image.
   */
-  (void) SetImageType(image,BilevelType,exception);
-  length=image->columns/8+(image->columns % 8 != 0 ? 1 : 0);
+  status=SetImageType(image,BilevelType,exception);
+  if (status == MagickFalse)
+    {
+      (void) CloseBlob(image);
+      return(MagickFalse);
+    }
+  length=(image->columns+7)/8;
   pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
   if (pixels == (unsigned char *) NULL)
     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
@@ -337,7 +371,10 @@ static MagickBooleanType WriteMONOImage(const ImageInfo *image_info,
   {
     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
     if (p == (const Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     bit=0;
     byte=0;
     q=pixels;