Commit 17d3dee48 for imagemagick.org

commit 17d3dee489ed57ee9b38b6c32fc7e3a6fab1d0cb
Author: Dirk Lemstra <dirk@lemstra.org>
Date:   Sun Dec 6 21:39:20 2026 +0100

    Encode per row in the sun encoder.

diff --git a/coders/sun.c b/coders/sun.c
index 4a96f0457..838b51c3b 100644
--- a/coders/sun.c
+++ b/coders/sun.c
@@ -993,19 +993,34 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
         {
           unsigned char
             bit,
-            byte;
+            byte,
+            *pixels,
+            *q;
+
+          size_t
+            length;

           /*
             Convert PseudoClass image to a SUN monochrome image.
           */
           (void) SetImageType(image,BilevelType,exception);
+          length=(image->columns+7)/8;
+          if (length & 0x01)
+            length++;
+          pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
+          if (pixels == (unsigned char *) NULL)
+            ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
           for (y=0; y < (ssize_t) image->rows; y++)
           {
             p=GetVirtualPixels(image,0,y,image->columns,1,exception);
             if (p == (const Quantum *) NULL)
-              break;
+              {
+                status=MagickFalse;
+                break;
+              }
             bit=0;
             byte=0;
+            q=pixels;
             for (x=0; x < (ssize_t) image->columns; x++)
             {
               byte<<=1;
@@ -1014,17 +1029,23 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
               bit++;
               if (bit == 8)
                 {
-                  (void) WriteBlobByte(image,byte);
+                  *q++=byte;
                   bit=0;
                   byte=0;
                 }
               p+=(ptrdiff_t) GetPixelChannels(image);
             }
             if (bit != 0)
-              (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
+              *q++=(unsigned char) (byte << (8-bit));
             if ((((image->columns/8)+
                 (image->columns % 8 ? 1 : 0)) % 2) != 0)
-              (void) WriteBlobByte(image,0);  /* pad scanline */
+              *q++=0;  /* pad scanline */
+            if (WriteBlob(image,(size_t) (q-pixels),pixels) !=
+                (ssize_t) (q-pixels))
+              {
+                status=MagickFalse;
+                break;
+              }
             if (image->previous == (Image *) NULL)
               {
                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
@@ -1033,9 +1054,19 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
                   break;
               }
           }
+          pixels=(unsigned char *) RelinquishMagickMemory(pixels);
+          if (status == MagickFalse)
+            break;
         }
       else
         {
+          unsigned char
+            *pixels,
+            *q;
+
+          size_t
+            length;
+
           /*
             Dump colormap to file.
           */
@@ -1051,19 +1082,32 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
           /*
             Convert PseudoClass packet to SUN colormapped pixel.
           */
+          length=image->columns+(image->columns & 0x01);
+          pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
+          if (pixels == (unsigned char *) NULL)
+            ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
           for (y=0; y < (ssize_t) image->rows; y++)
           {
             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++)
             {
-              (void) WriteBlobByte(image,(unsigned char)
-                GetPixelIndex(image,p));
+              *q++=(unsigned char) GetPixelIndex(image,p);
               p+=(ptrdiff_t) GetPixelChannels(image);
             }
             if (image->columns & 0x01)
-              (void) WriteBlobByte(image,0);  /* pad scanline */
+              *q++=0;  /* pad scanline */
+            if (WriteBlob(image,(size_t) (q-pixels),pixels) !=
+                (ssize_t) (q-pixels))
+              {
+                status=MagickFalse;
+                break;
+              }
             if (image->previous == (Image *) NULL)
               {
                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
@@ -1072,6 +1116,9 @@ static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
                   break;
               }
           }
+          pixels=(unsigned char *) RelinquishMagickMemory(pixels);
+          if (status == MagickFalse)
+            break;
         }
     if (GetNextImageInList(image) == (Image *) NULL)
       break;