Commit f90191be3 for imagemagick.org

commit f90191be33b40d9f4c1638c44e07a12646b6b116
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 17:11:45 2026 -0500

    a negative offset became a huge unsigned skip, so the reader scanned to end of file before failing

diff --git a/coders/pes.c b/coders/pes.c
index 928cd1d71..e796ac20a 100644
--- a/coders/pes.c
+++ b/coders/pes.c
@@ -433,9 +433,11 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
   ImageInfo
     *read_info;

-  int
+  double
     delta_x,
-    delta_y,
+    delta_y;
+
+  int
     j,
     unique_file,
     x,
@@ -493,7 +495,16 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if ((count != 4) || (LocaleNCompare((char *) magick,"#PES",4) != 0))
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   count=ReadBlob(image,4,version);
-  offset=ReadBlobLSBSignedLong(image);
+  if (count != 4)
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  /*
+    The PEC section offset is absolute; it must point past the 12-byte header
+    we have already consumed and lie within the blob.
+  */
+  offset=(ssize_t) ReadBlobLSBSignedLong(image);
+  if ((offset < 12) || ((GetBlobSize(image) != 0) &&
+      ((MagickSizeType) offset > GetBlobSize(image))))
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   if (DiscardBlobBytes(image,(MagickSizeType) (offset+36)) == MagickFalse)
     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
       image->filename);
@@ -502,7 +513,10 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
   /*
     Get PES colors.
   */
-  number_colors=(size_t) ReadBlobByte(image)+1;
+  j=ReadBlobByte(image);
+  if (j == EOF)
+    ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
+  number_colors=(size_t) j+1;
   for (i=0; i < (ssize_t) number_colors; i++)
   {
     j=ReadBlobByte(image);
@@ -533,12 +547,14 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
   bounds.y2=(-65535.0);
   i=0;
   j=0;
-  delta_x=0;
-  delta_y=0;
+  delta_x=0.0;
+  delta_y=0.0;
   while (EOFBlob(image) == MagickFalse)
   {
     x=ReadBlobByte(image);
     y=ReadBlobByte(image);
+    if ((x == EOF) || (y == EOF))
+      break;
     if ((x == 0xff) && (y == 0))
       break;
     if ((x == 254) && (y == 176))
@@ -547,12 +563,12 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
           Start a new stitch block.
         */
         j++;
-        blocks[j].offset=(ssize_t) i;
         if (j >= 255)
           {
             stitches=(PointInfo *) RelinquishMagickMemory(stitches);
-            ThrowReaderException(ResourceLimitError,"CorruptImage");
+            ThrowReaderException(CorruptImageError,"CorruptImage");
           }
+        blocks[j].offset=(ssize_t) i;
         (void) ReadBlobByte(image);
         continue;
       }
@@ -573,6 +589,8 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
         if ((x & 0x800) != 0)
           x-=0x1000;
         y=ReadBlobByte(image);
+        if (y == EOF)
+          break;
       }
     if ((y & 0x80) == 0)
       {
@@ -587,27 +605,39 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Jump stitch.
         */
-        y=((y & 0x0f) << 8)+ReadBlobByte(image);
+        int
+          low_byte;
+
+        low_byte=ReadBlobByte(image);
+        if (low_byte == EOF)
+          break;
+        y=((y & 0x0f) << 8)+low_byte;
         if ((y & 0x800) != 0)
           y-=0x1000;
       }
     /*
-      Note stitch (x,y).
+      Note stitch (x,y).  Accumulate in double precision so that a long run of
+      relative moves cannot overflow a signed int.
     */
-    x+=delta_x;
-    y+=delta_y;
-    delta_x=x;
-    delta_y=y;
-    stitches[i].x=(double) x;
-    stitches[i].y=(double) y;
-    if ((double) x < bounds.x1)
-      bounds.x1=(double) x;
-    if ((double) x > bounds.x2)
-      bounds.x2=(double) x;
-    if ((double) y < bounds.y1)
-      bounds.y1=(double) y;
-    if ((double) y > bounds.y2)
-      bounds.y2=(double) y;
+    delta_x+=(double) x;
+    delta_y+=(double) y;
+    stitches[i].x=delta_x;
+    stitches[i].y=delta_y;
+    if (i == 0)
+      {
+        bounds.x1=delta_x;
+        bounds.x2=delta_x;
+        bounds.y1=delta_y;
+        bounds.y2=delta_y;
+      }
+    if (delta_x < bounds.x1)
+      bounds.x1=delta_x;
+    if (delta_x > bounds.x2)
+      bounds.x2=delta_x;
+    if (delta_y < bounds.y1)
+      bounds.y1=delta_y;
+    if (delta_y > bounds.y2)
+      bounds.y2=delta_y;
     i++;
     if (i >= (ssize_t) number_stitches)
       {
@@ -621,6 +651,14 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
      }
   }
+  if (i == 0)
+    {
+      /*
+        No stitches: nothing to render (and stitches[0] would be uninitialized).
+      */
+      stitches=(PointInfo *) RelinquishMagickMemory(stitches);
+      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+    }
   j++;
   blocks[j].offset=(ssize_t) i;
   number_blocks=(size_t) j;
@@ -640,16 +678,26 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if (unique_file != -1)
     file=fdopen(unique_file,"wb");
   if ((unique_file == -1) || (file == (FILE *) NULL))
-    ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
+    {
+      if (unique_file != -1)
+        {
+          (void) close(unique_file);
+          (void) RelinquishUniqueFileResource(filename);
+        }
+      stitches=(PointInfo *) RelinquishMagickMemory(stitches);
+      ThrowReaderException(FileOpenError,"UnableToCreateTemporaryFile");
+    }
   (void) FormatLocaleFile(file,"<?xml version=\"1.0\"?>\n");
   (void) FormatLocaleFile(file,"<svg xmlns=\"http://www.w3.org/2000/svg\" "
-    "xlink=\"http://www.w3.org/1999/xlink\" "
-    "ev=\"http://www.w3.org/2001/xml-events\" version=\"1.1\" "
+    "xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
+    "xmlns:ev=\"http://www.w3.org/2001/xml-events\" version=\"1.1\" "
     "baseProfile=\"full\" width=\"%g\" height=\"%g\">\n",(double)
     image->columns,(double) image->rows);
   for (i=0; i < (ssize_t) number_blocks; i++)
   {
     offset=blocks[i].offset;
+    if ((blocks[i+1].offset-offset) <= 0)
+      continue;  /* empty block: no stitches to draw */
     (void) FormatLocaleFile(file,"  <path stroke=\"#%02x%02x%02x\" "
       "fill=\"none\" d=\"M %g %g",blocks[i].color->red,blocks[i].color->green,
       blocks[i].color->blue,stitches[offset].x-bounds.x1,
@@ -660,8 +708,12 @@ static Image *ReadPESImage(const ImageInfo *image_info,ExceptionInfo *exception)
     (void) FormatLocaleFile(file,"\"/>\n");
   }
   (void) FormatLocaleFile(file,"</svg>\n");
-  (void) fclose(file);
   stitches=(PointInfo *) RelinquishMagickMemory(stitches);
+  if (fclose(file) != 0)
+    {
+      (void) RelinquishUniqueFileResource(filename);
+      ThrowReaderException(FileOpenError,"UnableToWriteFile");
+    }
   (void) CloseBlob(image);
   image=DestroyImage(image);
   /*
@@ -713,7 +765,7 @@ ModuleExport size_t RegisterPESImage(void)
   MagickInfo
     *entry;

-  entry=AcquireMagickInfo("PES","PES","Embrid Embroidery Format");
+  entry=AcquireMagickInfo("PES","PES","Brother PES Embroidery Format");
   entry->decoder=(DecodeImageHandler *) ReadPESImage;
   entry->magick=(IsImageFormatHandler *) IsPES;
   (void) RegisterMagickInfo(entry);