Commit 23193928e for imagemagick.org

commit 23193928e0c625036d1b57593abe1e0165d48069
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 18:44:44 2026 -0500

    ReadMETAImage now checks the signed result of parse8BIM/parse8BIMW

diff --git a/coders/meta.c b/coders/meta.c
index acc7eaa00..1ee380d4c 100644
--- a/coders/meta.c
+++ b/coders/meta.c
@@ -543,7 +543,8 @@ static ssize_t parse8BIM(Image *ifile, Image *ofile)
     newstr=DestroyString(newstr);
   if (name != (char *) NULL)
     name=DestroyString(name);
-  line=DestroyString(line);
+  if (line != (char *) NULL)
+    line=DestroyString(line);
   if (savedolen > 0)
     {
       MagickOffsetType
@@ -865,7 +866,8 @@ static ssize_t parse8BIMW(Image *ifile, Image *ofile)
     newstr=DestroyString(newstr);
   if (name != (char *) NULL)
     name=DestroyString(name);
-  line=DestroyString(line);
+  if (line != (char *) NULL)
+    line=DestroyString(line);
   if (savedolen > 0)
     {
       MagickOffsetType
@@ -965,6 +967,8 @@ static int jpeg_skip_variable(Image *ifile, Image *ofile)
     return M_EOI;

   length = (((unsigned int) c1) << 8) + ((unsigned int) c2);
+  if (length < 2)
+    return M_EOI;  /* corrupt segment length */
   length -= 2;

   while (length--)
@@ -984,6 +988,8 @@ static int jpeg_skip_variable2(Image *ifile, Image *ofile)
   if ((c2 = ReadBlobByte(ifile)) == EOF) return M_EOI;

   length = (((unsigned int) c1) << 8) + ((unsigned int) c2);
+  if (length < 2)
+    return M_EOI;  /* corrupt segment length */
   length -= 2;

   while (length--)
@@ -1093,6 +1099,8 @@ static int jpeg_embed(Image *ifile, Image *ofile, Image *iptc)
             char
               psheader[] = "\xFF\xED\0\0Photoshop 3.0\0" "8BIM\x04\x04\0\0\0\0";

+            if (GetBlobSize(iptc) > (MagickSizeType) (0xFFFF-16-1))
+              return 0;  /* APP13 segment length is limited to 16 bits */
             len=(unsigned int) GetBlobSize(iptc);
             if (len & 1)
               len++; /* make the length even */
@@ -1256,7 +1264,15 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
       AttachBlob(buff->blob,blob,length);
       if (LocaleCompare(image_info->magick,"8BIMTEXT") == 0)
         {
-          length=(size_t) parse8BIM(image, buff);
+          ssize_t
+            extent;
+
+          /*
+            parse8BIM() returns -1 on error; casting that to size_t made it
+            look like a huge successful length.
+          */
+          extent=parse8BIM(image, buff);
+          length=(extent > 0) ? (size_t) extent : 0;
           if (length == 0)
             {
               blob=(unsigned char *) DetachBlob(buff->blob);
@@ -1269,7 +1285,11 @@ static Image *ReadMETAImage(const ImageInfo *image_info,
         }
       else if (LocaleCompare(image_info->magick,"8BIMWTEXT") == 0)
         {
-          length=(size_t) parse8BIMW(image, buff);
+          ssize_t
+            extent;
+
+          extent=parse8BIMW(image, buff);
+          length=(extent > 0) ? (size_t) extent : 0;
           if (length == 0)
             {
               blob=(unsigned char *) DetachBlob(buff->blob);
@@ -2009,8 +2029,8 @@ static int readWordFromBuffer(char **s, ssize_t *len)

   for (i=0; i<2; i++)
   {
+    if (*len <= 0) return -1;
     c = *(*s)++; (*len)--;
-    if (*len < 0) return -1;
     buffer[i] = (unsigned char) c;
   }
   return (((int) buffer[ 0 ]) <<  8) |
@@ -2063,11 +2083,11 @@ static int formatIPTCfromBuffer(Image *ofile, char *s, ssize_t len)
     /*
       We found the 0x1c tag and now grab the dataset and record number tags.
     */
+    if (len <= 0) return -1;
     c = *s++; len--;
-    if (len < 0) return -1;
     dataset = (unsigned char) c;
+    if (len <= 0) return -1;
     c = *s++; len--;
-    if (len < 0) return -1;
     recnum = (unsigned char) c;
     /* try to match this record to one of the ones in our named table */
     for (i=0; i< tagcount; i++)
@@ -2080,38 +2100,24 @@ static int formatIPTCfromBuffer(Image *ofile, char *s, ssize_t len)
     /*
       We decode the length of the block that follows - ssize_t or short fmt.
     */
-    c=(*s++);
-    len--;
-    if (len < 0)
+    if (len <= 0)
       return(-1);
+    c=(*s);
     if (c & (unsigned char) 0x80)
       return(0);
-    else
-      {
-        s--;
-        len++;
-        taglen=readWordFromBuffer(&s, &len);
-      }
+    taglen=readWordFromBuffer(&s, &len);
     if (taglen < 0)
       return(-1);
-    if (taglen > 65535)
+    if ((taglen > 65535) || (taglen > len))
       return(-1);
     /* make a buffer to hold the tag datand snag it from the input stream */
     str=(unsigned char *) AcquireQuantumMemory((size_t) (taglen+
       MagickPathExtent),sizeof(*str));
     if (str == (unsigned char *) NULL)
-      {
-        (void) printf("MemoryAllocationFailed");
-        return 0;
-      }
+      return(-1);
     for (tagindx=0; tagindx<taglen; tagindx++)
     {
       c = *s++; len--;
-      if (len < 0)
-        {
-          str=(unsigned char *) RelinquishMagickMemory(str);
-          return(-1);
-        }
       str[tagindx]=(unsigned char) c;
     }
     str[taglen]=0;