Commit 1e8a5f6c8 for imagemagick.org

commit 1e8a5f6c88d4adfd3444a8f31996f9fadbc80321
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat Nov 28 18:09:24 2026 -0500

    zero width or height and a colormapped image with zero colormap entries are now rejected

diff --git a/coders/tga.c b/coders/tga.c
index a25154e88..c2feb5a54 100644
--- a/coders/tga.c
+++ b/coders/tga.c
@@ -154,6 +154,7 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
   MagickBooleanType
     flip_x = MagickFalse,
     flip_y = MagickFalse,
+    skip = MagickFalse,
     status;

   PixelInfo
@@ -165,8 +166,7 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)

   size_t
     base,
-    flag,
-    skip;
+    flag;

   ssize_t
     count,
@@ -180,6 +180,7 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
     tga_info;

   unsigned char
+    bits,
     j,
     k,
     pixels[4],
@@ -234,6 +235,12 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if ((((tga_info.bits_per_pixel < 1) || (tga_info.bits_per_pixel >= 17)) &&
        (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  if ((tga_info.width == 0) || (tga_info.height == 0))
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  if (((tga_info.image_type == TGAColormap) ||
+       (tga_info.image_type == TGARLEColormap)) &&
+      (tga_info.colormap_length == 0))
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   /*
     Initialize image structure.
   */
@@ -270,23 +277,7 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
       (tga_info.image_type == TGARLERGB))
     image->compression=RLECompression;
   if (image->storage_class == PseudoClass)
-    {
-      if (tga_info.colormap_type != 0)
-        image->colors=tga_info.colormap_index+tga_info.colormap_length;
-      else
-        {
-          size_t
-            one;
-
-          one=1;
-          image->colors=one << tga_info.bits_per_pixel;
-          if ((MagickSizeType) image->colors > GetBlobSize(image))
-            ThrowReaderException(CorruptImageError,
-              "InsufficientImageDataInFile");
-          if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
-            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        }
-    }
+    image->colors=(size_t) tga_info.colormap_index+tga_info.colormap_length;
   if (tga_info.id_length != 0)
     {
       char
@@ -350,17 +341,64 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
       (void) CloseBlob(image);
       return(image);
     }
+  {
+    MagickOffsetType
+      position;
+
+    MagickSizeType
+      blob_size,
+      pixel_bytes,
+      pixel_count,
+      remaining;
+
+    /*
+      Reject a file that is far too small to hold the pixel data promised by
+      its header before a large pixel cache is allocated for it.
+    */
+    blob_size=GetBlobSize(image);
+    position=TellBlob(image);
+    if ((blob_size != 0) && (position >= 0) &&
+        ((MagickSizeType) position <= blob_size))
+      {
+        remaining=blob_size-(MagickSizeType) position;
+        pixel_count=(MagickSizeType) tga_info.width*tga_info.height;
+        if ((tga_info.image_type == TGARLEColormap) ||
+            (tga_info.image_type == TGARLERGB) ||
+            (tga_info.image_type == TGARLEMonochrome))
+          pixel_bytes=pixel_count/128;  /* a packet covers <= 128 pixels */
+        else
+          if (tga_info.bits_per_pixel == 1)
+            pixel_bytes=(MagickSizeType) tga_info.height*
+              (((MagickSizeType) tga_info.width+7)/8);
+          else
+            pixel_bytes=pixel_count*((tga_info.bits_per_pixel == 32) ? 4 :
+              (tga_info.bits_per_pixel == 24) ? 3 :
+              ((tga_info.bits_per_pixel == 15) ||
+               (tga_info.bits_per_pixel == 16)) ? 2 : 1);
+        if (pixel_bytes > remaining)
+          ThrowReaderException(CorruptImageError,
+            "InsufficientImageDataInFile");
+      }
+  }
   status=SetImageExtent(image,image->columns,image->rows,exception);
   if (status == MagickFalse)
     return(DestroyImageList(image));
   GetPixelInfo(image,&pixel);
-  if (tga_info.colormap_type != 0)
+  if ((tga_info.colormap_type != 0) && (image->storage_class != PseudoClass))
+    {
+      /*
+        The image does not use its colormap; skip over it so that the pixel
+        data is read from the right place.
+      */
+      if (DiscardBlobBytes(image,(MagickSizeType) tga_info.colormap_length*
+          (((MagickSizeType) tga_info.colormap_size+7)/8)) == MagickFalse)
+        ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
+    }
+  if (image->storage_class == PseudoClass)
     {
       /*
         Read TGA raster colormap.
       */
-      if (image->colors < tga_info.colormap_index)
-        image->colors=tga_info.colormap_index;
       if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       for (i=0; i < (ssize_t) tga_info.colormap_index; i++)
@@ -438,6 +476,7 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
   base=0;
   flag=0;
   skip=MagickFalse;
+  bits=0;
   index=0;
   runlength=0;
   offset=0;
@@ -487,16 +526,19 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 if (ReadBlob(image,1,pixels) != 1)
                   ThrowReaderException(CorruptImageError,
                     "UnableToReadImageData");
-                index=(Quantum) pixels[0];
+                bits=pixels[0];
               }
             else
-              index=(Quantum) ((size_t) index << 1);
-            if (tga_info.colormap_type != 0)
-              pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
-                ((unsigned char) index) & 0x80 ? 1 : 0,exception)];
+              bits=(unsigned char) (bits << 1);
+            if (image->storage_class == PseudoClass)
+              {
+                index=ConstrainColormapIndex(image,(bits & 0x80) != 0 ? 1 : 0,
+                  exception);
+                pixel=image->colormap[(ssize_t) index];
+              }
             else
               {
-                pixel.red=(MagickRealType) (((unsigned char) index) & 0x80 ?
+                pixel.red=(MagickRealType) ((bits & 0x80) != 0 ?
                   QuantumRange : 0);
                 pixel.green=pixel.red;
                 pixel.blue=pixel.red;
@@ -511,14 +553,15 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
             */
             if (ReadBlob(image,1,pixels) != 1)
               ThrowReaderException(CorruptImageError,"UnableToReadImageData");
-            index=(Quantum) pixels[0];
-            if (tga_info.colormap_type != 0)
-              pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
-                (ssize_t) index,exception)];
+            if (image->storage_class == PseudoClass)
+              {
+                index=ConstrainColormapIndex(image,(ssize_t) pixels[0],
+                  exception);
+                pixel=image->colormap[(ssize_t) index];
+              }
             else
               {
-                pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
-                  index);
+                pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[0]);
                 pixel.green=pixel.red;
                 pixel.blue=pixel.red;
               }
@@ -547,8 +590,11 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
               pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
                 TransparentAlpha : (Quantum) OpaqueAlpha);
             if (image->storage_class == PseudoClass)
-              index=(Quantum) ConstrainColormapIndex(image,((ssize_t) k << 8)+
-                j,exception);
+              {
+                index=ConstrainColormapIndex(image,((ssize_t) k << 8)+j,
+                  exception);
+                pixel=image->colormap[(ssize_t) index];
+              }
             break;
           }
           case 24:
@@ -577,8 +623,6 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
             break;
           }
         }
-      if (status == MagickFalse)
-        ThrowReaderException(CorruptImageError,"UnableToReadImageData");
       if (image->storage_class == PseudoClass)
         SetPixelIndex(image,index,q);
       SetPixelRed(image,ClampToQuantum(pixel.red),q);
@@ -633,21 +677,34 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
             Optional extension.
           */
           char
-            buffer[325] = { 0 };
+            buffer[325];

-          (void) ReadBlob(image,41,buffer);
-          buffer[41]='\0';
-          (void) SetImageProperty(image,"tga:author",buffer,exception);
-          (void) ReadBlob(image,324,buffer);
-          buffer[324]='\0';
-          (void) SetImageProperty(image,"tga:comment",buffer,exception);
+          int
+            attribute_type;
+
+          (void) memset(buffer,0,sizeof(buffer));
+          if (ReadBlob(image,41,buffer) == 41)
+            {
+              buffer[41]='\0';
+              (void) SetImageProperty(image,"tga:author",buffer,exception);
+            }
+          (void) memset(buffer,0,sizeof(buffer));
+          if (ReadBlob(image,324,buffer) == 324)
+            {
+              buffer[324]='\0';
+              (void) SetImageProperty(image,"tga:comment",buffer,exception);
+            }
           (void) DiscardBlobBytes(image,59);
-          (void) ReadBlob(image,41,buffer);
-          buffer[41]='\0';
-          (void) SetImageProperty(image,"tga:software",buffer,exception);
+          (void) memset(buffer,0,sizeof(buffer));
+          if (ReadBlob(image,41,buffer) == 41)
+            {
+              buffer[41]='\0';
+              (void) SetImageProperty(image,"tga:software",buffer,exception);
+            }
           (void) DiscardBlobBytes(image,27);
+          attribute_type=ReadBlobByte(image);
           if (((image->alpha_trait & BlendPixelTrait) != 0) &&
-              (ReadBlobByte(image) != 3))
+              (attribute_type >= 0) && (attribute_type != 3))
             image->alpha_trait=UndefinedPixelTrait;
         }
     }
@@ -767,7 +824,8 @@ ModuleExport void UnregisterTGAImage(void)
 %
 */
 static inline unsigned char *WriteTGAPixel(Image *image,TGAImageType image_type,
-  const Quantum *p,const QuantumAny range,const double midpoint,unsigned char *q)
+  const Quantum *p,const QuantumAny range,const double midpoint,
+  const MagickBooleanType depth_5,unsigned char *q)
 {
   if (image_type == TGAColormap || image_type == TGARLEColormap)
     *q++=(unsigned char) ((ssize_t) GetPixelIndex(image,p));
@@ -776,7 +834,7 @@ static inline unsigned char *WriteTGAPixel(Image *image,TGAImageType image_type,
       if (image_type == TGAMonochrome || image_type == TGARLEMonochrome)
         *q++=ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(image,p)));
       else
-        if (image->depth == 5)
+        if (depth_5 != MagickFalse)
           {
             unsigned char
               green,
@@ -822,6 +880,7 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
     *p;

   MagickBooleanType
+    depth_5,
     status;

   QuantumAny
@@ -870,6 +929,7 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
   if (image_info->compression != UndefinedCompression)
     compression=image_info->compression;
   range=GetQuantumRange(5UL);
+  depth_5=(image_info->depth == 5) ? MagickTrue : MagickFalse;
   tga_info.id_length=0;
   comment=GetImageProperty(image,"comment",exception);
   if (comment != (const char *) NULL)
@@ -902,7 +962,7 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
           Full color TGA raster.
         */
         tga_info.image_type=compression == RLECompression ? TGARLERGB : TGARGB;
-        if (image_info->depth == 5)
+        if (depth_5 != MagickFalse)
           {
             tga_info.bits_per_pixel=16;
             if (image->alpha_trait != UndefinedPixelTrait)
@@ -927,7 +987,7 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
           TGAColormap;
         tga_info.colormap_type=1;
         tga_info.colormap_length=(unsigned short) image->colors;
-        if (image_info->depth == 5)
+        if (depth_5 != MagickFalse)
           tga_info.colormap_size=16;
         else
           if (image->alpha_trait != UndefinedPixelTrait)
@@ -956,8 +1016,6 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
     default:
       break;
   }
-  if ((image->columns > 65535) || (image->rows > 65535))
-    ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
   /*
     Write TGA header.
   */
@@ -992,7 +1050,7 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
       q=targa_colormap;
       for (i=0; i < (ssize_t) image->colors; i++)
       {
-        if (image_info->depth == 5)
+        if (depth_5 != MagickFalse)
           {
             green=(unsigned char) ScaleQuantumToAny(ClampToQuantum(
               image->colormap[i].green),range);
@@ -1030,7 +1088,10 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
   {
     p=GetVirtualPixels(image,0,offset,image->columns,1,exception);
     if (p == (const Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     q=pixels;
     if (compression == RLECompression)
       {
@@ -1085,7 +1146,8 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
                   while (count >= 0)
                   {
                     q=WriteTGAPixel(image,tga_info.image_type,p-
-                      ((count+1)*(ssize_t) channels),range,midpoint,q);
+                      ((count+1)*(ssize_t) channels),range,midpoint,
+                      depth_5,q);
                     count--;
                   }
                   count=0;
@@ -1094,7 +1156,8 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
           if (i >= 3)
             {
               *q++=(unsigned char) ((i-1) | 0x80);
-              q=WriteTGAPixel(image,tga_info.image_type,p,range,midpoint,q);
+              q=WriteTGAPixel(image,tga_info.image_type,p,range,midpoint,
+                depth_5,q);
               p+=(ptrdiff_t) (i*(ssize_t) channels);
             }
           x+=i;
@@ -1103,7 +1166,8 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
     else
       for (x=0; x < (ssize_t) image->columns; x++)
       {
-        q=WriteTGAPixel(image,tga_info.image_type,p,range,midpoint,q);
+        q=WriteTGAPixel(image,tga_info.image_type,p,range,midpoint,depth_5,
+          q);
         p+=(ptrdiff_t) channels;
       }
     count=WriteBlob(image,(size_t) (q-pixels),pixels);