Commit ce4f205b8 for imagemagick.org

commit ce4f205b87174b5871a63ae350e562f7874e6877
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sun Nov 22 22:09:35 2026 -0500

    respect write error and return MagickFalse for LZWEncodeImage, PackbitsEncodeImage and ZLIBEncodeImage

diff --git a/MagickCore/compress.c b/MagickCore/compress.c
index d2be57880..269de62fa 100644
--- a/MagickCore/compress.c
+++ b/MagickCore/compress.c
@@ -317,6 +317,7 @@ static MagickBooleanType Ascii85EncodeInternal(Image *image,
     *p;

   ssize_t
+    i,
     n;

   assert(image != (Image *) NULL);
@@ -349,12 +350,14 @@ static MagickBooleanType Ascii85EncodeInternal(Image *image,
           break;
         }
     }
-    p+=(ptrdiff_t) 8;
+    p+=(ptrdiff_t) 4;
   }
+  /*
+    Carry any unconsumed bytes over to the start of the buffer.
+  */
   image->ascii85->offset=n;
-  p-=(ptrdiff_t)4;
-  for (n=0; n < 4; n++)
-    image->ascii85->buffer[n]=(*p++);
+  for (i=0; i < n; i++)
+    image->ascii85->buffer[i]=p[i];
   return(status);
 }

@@ -438,7 +441,8 @@ MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
     mask;

   MagickBooleanType
-    proceed;
+    proceed,
+    status;

   Quantum
     index;
@@ -472,6 +476,8 @@ MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   if (image->blob == (BlobInfo *) NULL)
     ThrowBinaryException(BlobError,"UnableToOpenBlob",image->filename);
+  if ((image->colormap == (PixelInfo *) NULL) || (image->colors < 2))
+    ThrowBinaryException(ImageError,"ImageTypeNotSupported",image->filename);
   mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash));
   mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash));
   scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
@@ -506,6 +512,7 @@ MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
   /*
     Uncompress 1D Huffman to runlength encoded pixels.
   */
+  status=MagickTrue;
   byte=0;
   mask=0;
   null_lines=0;
@@ -637,7 +644,10 @@ MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
     p=scanline;
     q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
     if (q == (Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
       index=(Quantum) (*p++);
@@ -646,15 +656,21 @@ MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
       q+=(ptrdiff_t) GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
       image->rows);
     if (proceed == MagickFalse)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     y++;
   }
   image_view=DestroyCacheView(image_view);
-  image->rows=(size_t) MagickMax((size_t) y-3,1);
+  image->rows=(size_t) MagickMax((ssize_t) y-3,1);
   image->compression=FaxCompression;
   /*
     Free decoder memory.
@@ -662,7 +678,7 @@ MagickExport MagickBooleanType HuffmanDecodeImage(Image *image,
   mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
   mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
-  return(MagickTrue);
+  return(status);
 }

 /*
@@ -819,7 +835,10 @@ RestoreMSCWarning \
       break;
     p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
     if (p == (const Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     for (x=0; x < (ssize_t) huffman_image->columns; x++)
     {
       *q++=(unsigned char) (GetPixelIntensity(huffman_image,p) >=
@@ -842,13 +861,22 @@ RestoreMSCWarning \
         q++;
         runlength++;
       }
+      while (runlength >= (2560+64))
+        {
+          /*
+            Runs beyond the largest make-up code need repeated 2560 codes.
+          */
+          entry=EXTable+((2560-1792)/64);
+          runlength-=(int) entry->count;
+          HuffmanOutputCode(entry);
+        }
       if (runlength >= 64)
         {
           if (runlength < 1792)
             entry=MWTable+((runlength/64)-1);
           else
             entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
-          runlength-=(long) entry->count;
+          runlength-=(int) entry->count;
           HuffmanOutputCode(entry);
         }
       entry=TWTable+MagickMin((size_t) runlength,63);
@@ -863,12 +891,19 @@ RestoreMSCWarning \
             q++;
             runlength++;
           }
+          while (runlength >= (2560+64))
+            {
+              entry=EXTable+((2560-1792)/64);
+              runlength-=(int) entry->count;
+              HuffmanOutputCode(entry);
+            }
           if (runlength >= 64)
             {
-              entry=MBTable+((runlength/64)-1);
-              if (runlength >= 1792)
+              if (runlength < 1792)
+                entry=MBTable+((runlength/64)-1);
+              else
                 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
-              runlength-=(long) entry->count;
+              runlength-=(int) entry->count;
               HuffmanOutputCode(entry);
             }
           entry=TBTable+MagickMin((size_t) runlength,63);
@@ -964,12 +999,13 @@ MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
 #define LZWEod  257UL  /* End of Data marker */
 #define OutputCode(code) \
 { \
-    accumulator+=code << (32-code_width-number_bits); \
+    accumulator+=(code) << (32-code_width-number_bits); \
     number_bits+=code_width; \
     while (number_bits >= 8) \
     { \
-      (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
-      accumulator=accumulator << 8; \
+      if (WriteBlobByte(image,(unsigned char) (accumulator >> 24)) != 1) \
+        status=MagickFalse; \
+      accumulator=(accumulator << 8) & 0xffffffffUL; \
       number_bits-=8; \
     } \
 }
@@ -992,6 +1028,9 @@ MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
     last_code,
     next_index;

+  MagickBooleanType
+    status;
+
   ssize_t
     index;

@@ -1010,11 +1049,12 @@ MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
   if (table == (TableType *) NULL)
-    ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed",
+    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
   /*
     Initialize variables.
   */
+  status=MagickTrue;
   accumulator=0;
   code_width=9;
   number_bits=0;
@@ -1028,7 +1068,8 @@ MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
   }
   next_index=LZWEod+1;
   code_width=9;
-  last_code=(size_t) pixels[0];
+  if (length != 0)
+    last_code=(size_t) pixels[0];
   for (i=1; i < (ssize_t) length; i++)
   {
     /*
@@ -1084,12 +1125,23 @@ MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
   /*
     Flush tables.
   */
-  OutputCode(last_code);
+  if (length != 0)
+    {
+      OutputCode(last_code);
+      /*
+        A decoder adds one more table entry after the final code, which can
+        widen its code size before it reads the EOD marker; stay in step.
+      */
+      next_index++;
+      if (((next_index >> code_width) != 0) && (code_width < 12))
+        code_width++;
+    }
   OutputCode(LZWEod);
   if (number_bits != 0)
-    (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
+    if (WriteBlobByte(image,(unsigned char) (accumulator >> 24)) != 1)
+      status=MagickFalse;
   table=(TableType *) RelinquishMagickMemory(table);
-  return(MagickTrue);
+  return(status);
 }

 /*
@@ -1110,7 +1162,7 @@ MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
 %  The format of the PackbitsEncodeImage method is:
 %
 %      MagickBooleanType PackbitsEncodeImage(Image *image,const size_t length,
-%        unsigned char *magick_restrict pixels)
+%        unsigned char *magick_restrict pixels,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -1121,17 +1173,27 @@ MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
 %    o pixels: the address of an unsigned array of characters containing the
 %      pixels to compress.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
   const size_t length,unsigned char *magick_restrict pixels,
   ExceptionInfo *exception)
 {
+#define PackbitsOutput(value) \
+{ \
+  if (WriteBlobByte(image,(unsigned char) (value)) != 1) \
+    status=MagickFalse; \
+}
+
   int
     count;

+  MagickBooleanType
+    status;
+
   ssize_t
-    i,
-    j;
+    i;

   unsigned char
     *packbits;
@@ -1148,23 +1210,24 @@ MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
   if (packbits == (unsigned char *) NULL)
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
-  for (i=(ssize_t) length; i != 0; )
+  status=MagickTrue;
+  for (i=(ssize_t) length; (i != 0) && (status != MagickFalse); )
   {
     switch (i)
     {
       case 1:
       {
         i--;
-        (void) WriteBlobByte(image,(unsigned char) 0);
-        (void) WriteBlobByte(image,*pixels);
+        PackbitsOutput(0);
+        PackbitsOutput(*pixels);
         break;
       }
       case 2:
       {
         i-=2;
-        (void) WriteBlobByte(image,(unsigned char) 1);
-        (void) WriteBlobByte(image,*pixels);
-        (void) WriteBlobByte(image,pixels[1]);
+        PackbitsOutput(1);
+        PackbitsOutput(*pixels);
+        PackbitsOutput(pixels[1]);
         break;
       }
       case 3:
@@ -1172,14 +1235,14 @@ MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
         i-=3;
         if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
           {
-            (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
-            (void) WriteBlobByte(image,*pixels);
+            PackbitsOutput((256-3)+1);
+            PackbitsOutput(*pixels);
             break;
           }
-        (void) WriteBlobByte(image,(unsigned char) 2);
-        (void) WriteBlobByte(image,*pixels);
-        (void) WriteBlobByte(image,pixels[1]);
-        (void) WriteBlobByte(image,pixels[2]);
+        PackbitsOutput(2);
+        PackbitsOutput(*pixels);
+        PackbitsOutput(pixels[1]);
+        PackbitsOutput(pixels[2]);
         break;
       }
       default:
@@ -1197,8 +1260,8 @@ MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
                 break;
             }
             i-=count;
-            (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
-            (void) WriteBlobByte(image,*pixels);
+            PackbitsOutput((256-count)+1);
+            PackbitsOutput(*pixels);
             pixels+=count;
             break;
           }
@@ -1216,16 +1279,16 @@ MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
         }
         i-=count;
         *packbits=(unsigned char) (count-1);
-        for (j=0; j <= (ssize_t) count; j++)
-          (void) WriteBlobByte(image,packbits[j]);
+        if (WriteBlob(image,(size_t) count+1,packbits) != (ssize_t) (count+1))
+          status=MagickFalse;
         pixels+=count;
         break;
       }
     }
   }
-  (void) WriteBlobByte(image,(unsigned char) 128);  /* EOD marker */
+  PackbitsOutput(128);  /* EOD marker */
   packbits=(unsigned char *) RelinquishMagickMemory(packbits);
-  return(MagickTrue);
+  return(status);
 }

 #if defined(MAGICKCORE_ZLIB_DELEGATE)
@@ -1279,10 +1342,11 @@ MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
   unsigned char *magick_restrict pixels,ExceptionInfo *exception)
 {
   int
+    level,
     status;

-  ssize_t
-    i;
+  MagickBooleanType
+    result;

   size_t
     compress_packets;
@@ -1297,37 +1361,66 @@ MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
   assert(image->signature == MagickCoreSignature);
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  compress_packets=(size_t) (1.001*length+12);
-  compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
-    sizeof(*compress_pixels));
-  if (compress_pixels == (unsigned char *) NULL)
+  /*
+    zlib counts bytes in an unsigned int.
+  */
+  if ((MagickSizeType) length > (MagickSizeType) 0xffffffffUL)
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
+  level=(int) (image->quality == UndefinedCompressionQuality ? 7 :
+    MagickMin(image->quality/10,9));
   (void) memset(&stream,0,sizeof(stream));
+  stream.zalloc=AcquireZIPMemory;
+  stream.zfree=RelinquishZIPMemory;
+  stream.opaque=(voidpf) NULL;
+  status=deflateInit(&stream,level);
+  if (status != Z_OK)
+    ThrowBinaryException(CoderError,"UnableToZipCompressImage",
+      image->filename);
+  compress_packets=(size_t) deflateBound(&stream,(uLong) length);
+  if ((MagickSizeType) compress_packets > (MagickSizeType) 0xffffffffUL)
+    {
+      (void) deflateEnd(&stream);
+      ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
+        image->filename);
+    }
+  compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
+    sizeof(*compress_pixels));
+  if (compress_pixels == (unsigned char *) NULL)
+    {
+      (void) deflateEnd(&stream);
+      ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
+        image->filename);
+    }
   stream.next_in=pixels;
   stream.avail_in=(unsigned int) length;
   stream.next_out=compress_pixels;
   stream.avail_out=(unsigned int) compress_packets;
-  stream.zalloc=AcquireZIPMemory;
-  stream.zfree=RelinquishZIPMemory;
-  stream.opaque=(voidpf) NULL;
-  status=deflateInit(&stream,(int) (image->quality ==
-    UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
-  if (status == Z_OK)
+  status=deflate(&stream,Z_FINISH);
+  compress_packets=(size_t) stream.total_out;
+  if (status != Z_STREAM_END)
     {
-      status=deflate(&stream,Z_FINISH);
-      if (status == Z_STREAM_END)
-        status=deflateEnd(&stream);
-      else
-        (void) deflateEnd(&stream);
-      compress_packets=(size_t) stream.total_out;
+      /*
+        Anything but Z_STREAM_END means the stream is incomplete.
+      */
+      (void) deflateEnd(&stream);
+      compress_pixels=(unsigned char *) RelinquishMagickMemory(
+        compress_pixels);
+      ThrowBinaryException(CoderError,"UnableToZipCompressImage",
+        image->filename);
     }
+  status=deflateEnd(&stream);
   if (status != Z_OK)
-    ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
-  for (i=0; i < (ssize_t) compress_packets; i++)
-    (void) WriteBlobByte(image,compress_pixels[i]);
+    {
+      compress_pixels=(unsigned char *) RelinquishMagickMemory(
+        compress_pixels);
+      ThrowBinaryException(CoderError,"UnableToZipCompressImage",
+        image->filename);
+    }
+  result=WriteBlob(image,compress_packets,compress_pixels) ==
+    (ssize_t) compress_packets ? MagickTrue : MagickFalse;
   compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
-  return(MagickTrue);
+  return(result);
 }
 #else
 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,