Commit 376fc2f0c for imagemagick.org

commit 376fc2f0c1f09d4e22b3ddbff05fbb0492d8b821
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 18:43:27 2026 -0500

    WriteMAPImage now sizes the index row by the bytes it actually writes per pixel

diff --git a/coders/map.c b/coders/map.c
index b09a1377c..2043c43d5 100644
--- a/coders/map.c
+++ b/coders/map.c
@@ -162,7 +162,11 @@ static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
   depth=GetImageQuantumDepth(image,MagickTrue);
   if ((depth <= 8) && (image->colors > 256))
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-  packet_size=(size_t) (depth/8);
+  /*
+    Indexes are 1 byte for <= 256 colors and 2 bytes otherwise, matching the
+    way they are decoded below and the way WriteMAPImage() emits them.
+  */
+  packet_size=(size_t) (image->colors > 256 ? 2UL : 1UL);
   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
     sizeof(*pixels));
   packet_size=(size_t) (depth > 8 ? 6UL : 3UL);
@@ -171,8 +175,10 @@ static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if ((pixels == (unsigned char *) NULL) ||
       (colormap == (unsigned char *) NULL))
     {
-      pixels=(unsigned char *) RelinquishMagickMemory(pixels);
-      colormap=(unsigned char *) RelinquishMagickMemory(colormap);
+      if (pixels != (unsigned char *) NULL)
+        pixels=(unsigned char *) RelinquishMagickMemory(pixels);
+      if (colormap != (unsigned char *) NULL)
+        colormap=(unsigned char *) RelinquishMagickMemory(colormap);
       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
     }
   /*
@@ -186,7 +192,7 @@ static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
       ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
     }
   p=colormap;
-  if (image->depth <= 8)
+  if (depth <= 8)
     for (i=0; i < (ssize_t) image->colors; i++)
     {
       image->colormap[i].red=ScaleCharToQuantum(*p++);
@@ -197,14 +203,17 @@ static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
     for (i=0; i < (ssize_t) image->colors; i++)
     {
       quantum=(size_t) (*p++ << 8);
-      quantum|=(*p++);
-      image->colormap[i].red=(Quantum) quantum;
+      quantum|=(size_t) (*p++);
+      image->colormap[i].red=(double) ScaleShortToQuantum((unsigned short)
+        quantum);
       quantum=(size_t) (*p++ << 8);
       quantum|=(size_t) (*p++);
-      image->colormap[i].green=(Quantum) quantum;
+      image->colormap[i].green=(double) ScaleShortToQuantum((unsigned short)
+        quantum);
       quantum=(size_t) (*p++ << 8);
-      quantum|=(*p++);
-      image->colormap[i].blue=(Quantum) quantum;
+      quantum|=(size_t) (*p++);
+      image->colormap[i].blue=(double) ScaleShortToQuantum((unsigned short)
+        quantum);
     }
   colormap=(unsigned char *) RelinquishMagickMemory(colormap);
   if (image_info->ping != MagickFalse)
@@ -222,7 +231,7 @@ static Image *ReadMAPImage(const ImageInfo *image_info,ExceptionInfo *exception)
   /*
     Read image pixels.
   */
-  packet_size=(size_t) (depth/8);
+  packet_size=(size_t) (image->colors > 256 ? 2UL : 1UL);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     p=pixels;
@@ -399,8 +408,14 @@ static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image,
   */
   if (SetImageType(image,PaletteType,exception) == MagickFalse)
     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+  /*
+    Size the index row by the number of bytes actually emitted per pixel
+    (2 when colors > 256).  Sizing it by the quantum depth under-allocated
+    the row for 8-bit images with more than 256 colors (heap overflow).
+  */
   depth=GetImageQuantumDepth(image,MagickTrue);
-  packet_size=(size_t) (depth/8);
+  (void) depth;
+  packet_size=(size_t) (image->colors > 256 ? 2UL : 1UL);
   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size*
     sizeof(*pixels));
   packet_size=(size_t) (image->colors > 256 ? 6UL : 3UL);
@@ -438,19 +453,24 @@ static MagickBooleanType WriteMAPImage(const ImageInfo *image_info,Image *image,
       *q++=(unsigned char) (ScaleQuantumToShort((Quantum) image->colormap[i].blue) &
         0xff);
     }
-  (void) WriteBlob(image,packet_size*image->colors,colormap);
+  if (WriteBlob(image,packet_size*image->colors,colormap) !=
+      (ssize_t) (packet_size*image->colors))
+    status=MagickFalse;
   colormap=(unsigned char *) RelinquishMagickMemory(colormap);
   /*
     Write image pixels to file.
   */
-  for (y=0; y < (ssize_t) image->rows; y++)
+  for (y=0; (status != MagickFalse) && (y < (ssize_t) image->rows); y++)
   {
     size_t
       count;

     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
     if (p == (const Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     q=pixels;
     for (x=0; x < (ssize_t) image->columns; x++)
     {