Commit 81ad7eaae for imagemagick.org

commit 81ad7eaae0d146bde6cf766b27194d9a206239d2
Author: Dirk Lemstra <dirk@lemstra.org>
Date:   Fri Nov 27 23:45:43 2026 +0100

    Corrected the patch to check for incomplete writes.

diff --git a/coders/psd.c b/coders/psd.c
index 51733896b..ac3eeeb81 100644
--- a/coders/psd.c
+++ b/coders/psd.c
@@ -3092,7 +3092,8 @@ static size_t WritePSDChannel(const PSDInfo *psd_info,
     *p;

   MagickBooleanType
-    monochrome;
+    monochrome,
+    status;

   QuantumInfo
     *quantum_info;
@@ -3103,6 +3104,7 @@ static size_t WritePSDChannel(const PSDInfo *psd_info,
   ssize_t
     count,
     i,
+    written,
     y;

   unsigned char
@@ -3124,6 +3126,7 @@ static size_t WritePSDChannel(const PSDInfo *psd_info,
   flush=Z_NO_FLUSH;
 #endif
   count=0;
+  status=MagickTrue;
   if (separate != MagickFalse)
     {
       size_offset=TellBlob(image)+2;
@@ -3178,7 +3181,13 @@ static size_t WritePSDChannel(const PSDInfo *psd_info,
       {
         length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
           exception);
-        count+=WriteBlob(image,length,compact_pixels);
+        written=WriteBlob(image,length,compact_pixels);
+        if (written != (ssize_t) length)
+          {
+            status=MagickFalse;
+            break;
+          }
+        count+=written;
         size_offset+=WritePSDOffset(psd_info,image,length,size_offset);
       }
 #ifdef MAGICKCORE_ZLIB_DELEGATE
@@ -3195,12 +3204,30 @@ static size_t WritePSDChannel(const PSDInfo *psd_info,
               break;
             length=(size_t) MagickMinBufferExtent-stream.avail_out;
             if (length > 0)
-              count+=WriteBlob(image,length,compressed_pixels);
+              {
+                written=WriteBlob(image,length,compressed_pixels);
+                if (written != (ssize_t) length)
+                  {
+                    status=MagickFalse;
+                    break;
+                  }
+                count+=written;
+              }
         } while (stream.avail_out == 0);
+        if (status == MagickFalse)
+          break;
       }
 #endif
     else
-      count+=WriteBlob(image,length,pixels);
+      {
+        written=WriteBlob(image,length,pixels);
+        if (written != (ssize_t) length)
+          {
+            status=MagickFalse;
+            break;
+          }
+        count+=written;
+      }
   }
 #ifdef MAGICKCORE_ZLIB_DELEGATE
   if (compression == ZipCompression)
@@ -3211,7 +3238,7 @@ static size_t WritePSDChannel(const PSDInfo *psd_info,
     }
 #endif
   quantum_info=DestroyQuantumInfo(quantum_info);
-  return((size_t) count);
+  return(status != MagickFalse ? (size_t) count : 0);
 }

 static unsigned char *AcquireCompactPixels(const Image *image,
@@ -3290,8 +3317,6 @@ static size_t WritePSDChannels(const PSDInfo *psd_info,
         (ssize_t) channels);
       offset_length=(layer->rows*(psd_info->version == 1 ? 2 : 4));
     }
-  if (total_length == 0)
-    return(0);
   size_offset+=2;
   if ((layer->storage_class == PseudoClass) &&
       (IsImageGray(layer) == MagickFalse))