Commit 3ad3c5d46 for imagemagick.org

commit 3ad3c5d4622246dbdb75507ee1e7a8d112b66f41
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat Dec 5 11:21:12 2026 -0500

    SyncExifProfile() rejects EXIF entries of format 0 + SetImageProperty() rejects a NULL property

diff --git a/MagickCore/profile-private.h b/MagickCore/profile-private.h
index 03d99c378..340b03a51 100644
--- a/MagickCore/profile-private.h
+++ b/MagickCore/profile-private.h
@@ -24,16 +24,26 @@
 extern "C" {
 #endif

+/*
+  SetImageProfilePrivate() takes ownership of the StringInfo in every case:
+  it is stored in the image or destroyed (on rejection or failure), so the
+  caller must not touch it afterwards.  The profile name is taken from the
+  StringInfo itself (SetStringInfoName()).
+*/
 extern MagickExport MagickBooleanType
   SetImageProfilePrivate(Image *,StringInfo *,ExceptionInfo *);

 extern MagickExport size_t
   GetMaxProfileCount(void);

+/*
+  Both return NULL (with a ResourceLimitWarning) when the length exceeds the
+  policy's maximum profile size; the result is named and zero-padded.
+*/
 extern MagickExport StringInfo
-  *AcquireProfileStringInfo(const char *,const size_t length,ExceptionInfo *),
-  *BlobToProfileStringInfo(const char *,const void *blob,const size_t length,
-    ExceptionInfo *exception);
+  *AcquireProfileStringInfo(const char *,const size_t,ExceptionInfo *),
+  *BlobToProfileStringInfo(const char *,const void *,const size_t,
+    ExceptionInfo *);

 extern MagickPrivate void
   AppendImageProfileProperty(Image *,const char *,const char *,const char *,
diff --git a/MagickCore/profile.c b/MagickCore/profile.c
index 1bec4de86..53a4e8e81 100644
--- a/MagickCore/profile.c
+++ b/MagickCore/profile.c
@@ -302,6 +302,8 @@ MagickExport MagickBooleanType DeleteImageProfile(Image *image,const char *name)
 */
 MagickExport void DestroyImageProfiles(Image *image)
 {
+  assert(image != (Image *) NULL);
+  assert(image->signature == MagickCoreSignature);
   if (image->profiles != (SplayTreeInfo *) NULL)
     image->profiles=DestroySplayTree((SplayTreeInfo *) image->profiles);
 }
@@ -1759,6 +1761,9 @@ static void WriteTo8BimProfile(Image *image,const char *name,
   if ((image->profiles == (SplayTreeInfo *) NULL) ||
       (name == (const char *) NULL))
     return;
+  if ((profile != (const StringInfo *) NULL) &&
+      ((MagickSizeType) profile->length >= (MagickSizeType) 0xffffffffUL))
+    return;  /* 8BIM resource sizes are 32-bit (and padded to even) */
   if (LocaleCompare(name,"icc") == 0)
     profile_id=0x040f;
   else
@@ -2168,6 +2173,20 @@ static MagickBooleanType SetImageProfileInternal(Image *image,const char *name,
       return(MagickTrue);
     }
   PatchCorruptProfile(name,profile);
+  /*
+    The repair can change the length (EXIF gains a 6-byte header, XMP loses
+    trailing garbage): re-validate and use the new length from here on.
+  */
+  length=GetStringInfoLength(profile);
+  if ((length == 0) || (length > GetMaxProfileSize()))
+    {
+      if (length != 0)
+        (void) ThrowMagickException(exception,GetMagickModule(),
+          ResourceLimitWarning,"ProfileSizeExceedsLimit","`%llu'",
+          (unsigned long long) length);
+      profile=DestroyStringInfo(profile);
+      return(MagickTrue);
+    }
   if ((LocaleCompare(name,"xmp") == 0) &&
       (ValidateXMPProfile(image,profile,exception) == MagickFalse))
     {
@@ -2590,8 +2609,8 @@ static void SyncExifProfile(const Image *image,unsigned char *exif,
       (void) AddValueToSplayTree(exif_resources,q,q);
       tag_value=(unsigned short) ReadProfileShort(endian,q);
       format=(ssize_t) ReadProfileShort(endian,q+2);
-      if ((format < 0) || ((format-1) >= EXIF_NUM_FORMATS))
-        break;
+      if ((format < 1) || (format > EXIF_NUM_FORMATS))
+        break;  /* corrupt EXIF: format 0 is not a TIFF type */
       components=(int) ReadProfileLong(endian,q+4);
       if (components < 0)
         break;  /* corrupt EXIF */
@@ -2651,27 +2670,31 @@ static void SyncExifProfile(const Image *image,unsigned char *exif,
         }
         case 0x0112:
         {
-          if (directory != ifd0)
+          /*
+            Orientation: rewrite only a single SHORT or LONG value; any other
+            type/count would be overwritten with a mis-sized integer.
+          */
+          if ((directory != ifd0) || (components != 1))
             break;
-          if (number_bytes == 4)
-            {
-              (void) WriteProfileLong(endian,(size_t) image->orientation,p);
-              break;
-            }
-          (void) WriteProfileShort(endian,(unsigned short) image->orientation,
-            p);
+          if (format == 4)
+            WriteProfileLong(endian,(size_t) image->orientation,p);
+          else
+            if (format == 3)
+              WriteProfileShort(endian,(unsigned short) image->orientation,p);
           break;
         }
         case 0x0128:
         {
-          if (directory != ifd0)
+          /*
+            ResolutionUnit: same single SHORT/LONG restriction.
+          */
+          if ((directory != ifd0) || (components != 1))
             break;
-          if (number_bytes == 4)
-            {
-              (void) WriteProfileLong(endian,((size_t) image->units)+1,p);
-              break;
-            }
-          (void) WriteProfileShort(endian,(unsigned short) (image->units+1),p);
+          if (format == 4)
+            WriteProfileLong(endian,((size_t) image->units)+1,p);
+          else
+            if (format == 3)
+              WriteProfileShort(endian,(unsigned short) (image->units+1),p);
           break;
         }
         default:
diff --git a/MagickCore/profile.h b/MagickCore/profile.h
index faf509b79..776af8554 100644
--- a/MagickCore/profile.h
+++ b/MagickCore/profile.h
@@ -36,6 +36,13 @@ typedef enum
   RelativeIntent
 } RenderingIntent;

+/*
+  Ownership: GetNextImageProfile() and GetImageProfile() return storage owned
+  by the image (do not free; invalid after the profile is set, removed, or the
+  image destroyed).  RemoveImageProfile() hands the profile to the caller, who
+  must release it with DestroyStringInfo().  SetImageProfile() copies its
+  argument.
+*/
 extern MagickExport char
   *GetNextImageProfile(const Image *);

diff --git a/MagickCore/property.c b/MagickCore/property.c
index d51737a1e..efc9e9444 100644
--- a/MagickCore/property.c
+++ b/MagickCore/property.c
@@ -295,7 +295,8 @@ MagickExport MagickBooleanType DeleteImageProperty(Image *image,
   assert(image->signature == MagickCoreSignature);
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  if (image->properties == (void *) NULL)
+  if ((image->properties == (void *) NULL) ||
+      (property == (const char *) NULL))
     return(MagickFalse);
   return(DeleteNodeFromSplayTree((SplayTreeInfo *) image->properties,property));
 }
@@ -689,7 +690,15 @@ static MagickBooleanType Get8BIMProperty(const Image *image,const char *key,
     if (resource != (char *) NULL)
       resource=DestroyString(resource);
     count=(ssize_t) ReadPropertyByte(&info,&length);
-    if ((count != 0) && ((size_t) count <= length))
+    if ((count < 0) || ((size_t) count > length))
+      {
+        /*
+          Truncated Pascal resource name: nothing sane follows.
+        */
+        length=0;
+        continue;
+      }
+    if (count != 0)
       {
         if (~((size_t) count) >= (MagickPathExtent-1))
           resource=(char *) AcquireQuantumMemory((size_t) count+
@@ -700,6 +709,8 @@ static MagickBooleanType Get8BIMProperty(const Image *image,const char *key,
               resource[i]=(char) ReadPropertyByte(&info,&length);
             resource[count]='\0';
           }
+        else
+          SkipPropertyBytes(&info,&length,(size_t) count);  /* stay aligned */
       }
     if ((count & 0x01) == 0)
       (void) ReadPropertyByte(&info,&length);
@@ -772,7 +783,7 @@ static MagickBooleanType Get8BIMProperty(const Image *image,const char *key,
     macroman_resource=DestroyString(macroman_resource);
   if (resource != (char *) NULL)
     resource=DestroyString(resource);
-  return(MagickTrue);
+  return(status);
 }

 static inline signed int ReadPropertySignedLong(const EndianType endian,
@@ -2090,7 +2101,11 @@ static char *TracePSClippath(const unsigned char *blob,size_t length)
   (void) memset(last,0,sizeof(last));
   knot_count=0;
   in_subpath=MagickFalse;
-  while (length > 0)
+  /*
+    Path records are always 26 bytes (2-byte selector + 24-byte payload); a
+    truncated trailing record would otherwise emit bogus (-1,-1) knots.
+  */
+  while (length >= 26)
   {
     selector=(ssize_t) ReadPropertyMSBShort(&blob,&length);
     switch (selector)
@@ -2286,7 +2301,7 @@ static char *TraceSVGClippath(const unsigned char *blob,size_t length,
   (void) memset(last,0,sizeof(last));
   knot_count=0;
   in_subpath=MagickFalse;
-  while (length != 0)
+  while (length >= 26)  /* whole 26-byte path records only */
   {
     selector=(ssize_t) ReadPropertyMSBShort(&blob,&length);
     switch (selector)
@@ -3359,6 +3374,7 @@ MagickExport const char *GetMagickProperty(ImageInfo *image_info,
           const MagickInfo
             *magick_info;

+          WarnNoImageReturn("\"%%[%s]\"",property);
           magick_info=GetMagickInfo(image->magick,exception);
           if ((magick_info != (const MagickInfo *) NULL) &&
               (GetMagickMimeType(magick_info) != (const char *) NULL))
@@ -3493,7 +3509,8 @@ MagickExport const char *GetMagickProperty(ImageInfo *image_info,
 #endif
               (void) cmsCloseProfile(icc_profile);
             }
-      }
+          break;
+        }
 #endif
       if (LocaleCompare("printsize.x",property) == 0)
         {
@@ -3689,6 +3706,7 @@ MagickExport const char *GetMagickProperty(ImageInfo *image_info,
   return((char *) NULL);
 }
 #undef WarnNoImageReturn
+#undef WarnNoImageInfoReturn

 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -3781,12 +3799,24 @@ MagickExport const char *GetNextImageProperty(const Image *image)
 MagickExport char *InterpretImageProperties(ImageInfo *image_info,Image *image,
   const char *embed_text,ExceptionInfo *exception)
 {
+/*
+  The buffer holds extent+MagickPathExtent bytes.  The write offset is kept
+  explicitly across a resize (the old code re-derived it with strlen(), which
+  silently relied on the text being NUL terminated at the cursor), growth is
+  geometric, and every write is bounded by the space actually left.
+*/
+#define InterpretTextRoom() \
+  (extent+MagickPathExtent-(size_t) (q-interpret_text))
+
 #define ExtendInterpretText(string_length) \
 { \
-  size_t length=(string_length); \
-  if ((size_t) (q-interpret_text+(ssize_t) length+1) >= extent) \
+  size_t \
+    length=(string_length), \
+    used=(size_t) (q-interpret_text); \
+ \
+  if ((used+length+1) >= extent) \
     { \
-      extent+=length; \
+      extent=MagickMax(2*extent,used+length+1); \
       interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
         MagickPathExtent,sizeof(*interpret_text)); \
       if (interpret_text == (char *) NULL) \
@@ -3794,45 +3824,24 @@ MagickExport char *InterpretImageProperties(ImageInfo *image_info,Image *image,
           status=MagickFalse; \
           goto cleanup; \
         } \
-      q=interpret_text+strlen(interpret_text); \
-   } \
+      q=interpret_text+used; \
+    } \
 }

 #define AppendKeyValue2Text(key,value)\
 { \
-  size_t length=strlen(key)+strlen(value)+2; \
-  if ((size_t) (q-interpret_text+(ssize_t) length+1) >= extent) \
-    { \
-      extent+=length; \
-      interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
-        MagickPathExtent,sizeof(*interpret_text)); \
-      if (interpret_text == (char *) NULL) \
-        { \
-          status=MagickFalse; \
-          goto cleanup; \
-        } \
-      q=interpret_text+strlen(interpret_text); \
-     } \
-   q+=(ptrdiff_t) FormatLocaleString(q,extent,"%s=%s\n",(key),(value)); \
+  ExtendInterpretText(strlen(key)+strlen(value)+2); \
+  q+=(ptrdiff_t) FormatLocaleString(q,InterpretTextRoom(),"%s=%s\n",(key), \
+    (value)); \
 }

 #define AppendString2Text(string) \
 { \
-  size_t length = strlen((string)); \
-  if ((size_t) (q-interpret_text+(ssize_t) length+1) >= extent) \
-    { \
-      extent+=length; \
-      interpret_text=(char *) ResizeQuantumMemory(interpret_text,extent+ \
-        MagickPathExtent,sizeof(*interpret_text)); \
-      if (interpret_text == (char *) NULL) \
-        { \
-          status=MagickFalse; \
-          goto cleanup; \
-        } \
-      q=interpret_text+strlen(interpret_text); \
-    } \
-  (void) CopyMagickString(q,(string),extent); \
-  q+=(ptrdiff_t) length; \
+  size_t string_extent = strlen((string)); \
+ \
+  ExtendInterpretText(string_extent); \
+  (void) CopyMagickString(q,(string),InterpretTextRoom()); \
+  q+=(ptrdiff_t) string_extent; \
 }

   char
@@ -3914,7 +3923,7 @@ MagickExport char *InterpretImageProperties(ImageInfo *image_info,Image *image,
       status=MagickFalse;
       goto cleanup;
     }
-  extent=MagickPathExtent;                  /* allocated space in string */
+  extent=strlen(embed_text);  /* AcquireString() added MagickPathExtent */
   number=MagickFalse;                       /* is last char a number? */
   for (q=interpret_text; *p!='\0'; number=isdigit((int) ((unsigned char) *p)) ? MagickTrue : MagickFalse,p++)
   {
@@ -4053,10 +4062,13 @@ MagickExport char *InterpretImageProperties(ImageInfo *image_info,Image *image,
       depth=1;
       if (*p == ']')
         {
+          /*
+            An empty "%[]" is an unknown property, like any other: warn and
+            keep interpreting rather than discarding the whole string.
+          */
           (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
             "UnknownImageProperty","\"%%[]\"");
-          status=MagickFalse;
-          goto cleanup;
+          continue;
         }
       for (offset=0; offset < (MagickPathExtent-1L) && (*p != '\0'); )
       {
@@ -4422,7 +4434,8 @@ MagickExport char *RemoveImageProperty(Image *image,const char *property)
   assert(image->signature == MagickCoreSignature);
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  if (image->properties == (void *) NULL)
+  if ((image->properties == (void *) NULL) ||
+      (property == (const char *) NULL))
     return((char *) NULL);
   value=(char *) RemoveNodeFromSplayTree((SplayTreeInfo *) image->properties,
     property);
@@ -4513,6 +4526,16 @@ MagickExport MagickBooleanType SetImageProperty(Image *image,
   assert(image->signature == MagickCoreSignature);
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  if ((property == (const char *) NULL) || (*property == '\0'))
+    {
+      /*
+        A NULL or empty key cannot be stored (strlen() and the splay-tree
+        string compare would dereference NULL).
+      */
+      (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
+        "SetReadOnlyProperty","`%s'","(null)");
+      return(MagickFalse);
+    }
   if (image->properties == (void *) NULL)
     image->properties=NewSplayTree(CompareSplayTreeString,
       RelinquishMagickMemory,RelinquishMagickMemory);  /* create splay-tree */
diff --git a/MagickCore/property.h b/MagickCore/property.h
index 5219f7d9a..3e4741688 100644
--- a/MagickCore/property.h
+++ b/MagickCore/property.h
@@ -22,6 +22,14 @@
 extern "C" {
 #endif

+/*
+  Ownership: InterpretImageProperties() and RemoveImageProperty() return
+  caller-owned strings (release with DestroyString()).  The const char *
+  results of GetImageProperty(), GetMagickProperty() and
+  GetNextImageProperty() are owned by the image/image_info and may be
+  invalidated by the next property set/delete or by the next
+  GetMagickProperty() call; copy them if they must outlive that.
+*/
 extern MagickExport char
   *InterpretImageProperties(ImageInfo *,Image *,const char *,
     ExceptionInfo *),