Commit 1c74e1405 for imagemagick.org

commit 1c74e1405d96d2eecebf175fd2835a113927ce83
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 18:42:11 2026 -0500

    WriteHTMLImage now builds the FTP/HTTP URL prefix with a bounded copy

diff --git a/coders/html.c b/coders/html.c
index 6b70424f2..a37b0cef2 100644
--- a/coders/html.c
+++ b/coders/html.c
@@ -213,9 +213,13 @@ static void WriteHtmlEncodedString(Image *image,const char* value)
     *encoded_value;

   encoded_value=AcquireString(value);
+  /*
+    Escape '&' first, otherwise the entities introduced below are re-escaped
+    (e.g. "<" would become "&amp;lt;").
+  */
+  (void) SubstituteString(&encoded_value,"&","&amp;");
   (void) SubstituteString(&encoded_value,"<","&lt;");
   (void) SubstituteString(&encoded_value,">","&gt;");
-  (void) SubstituteString(&encoded_value,"&","&amp;");
   (void) SubstituteString(&encoded_value,"\"","&quot;");
   (void) SubstituteString(&encoded_value,"'","&apos;");
   WriteBlobString(image,encoded_value);
@@ -291,12 +295,25 @@ static MagickBooleanType WriteHTMLImage(const ImageInfo *image_info,
       p=strrchr(image->filename,'/');
       if (p != (char *) NULL)
         {
+          size_t
+            extent,
+            prefix;
+
           p++;
           (void) CopyMagickString(url,image_info->magick,MagickPathExtent);
           (void) ConcatenateMagickString(url,":",MagickPathExtent);
-          url[strlen(url)+p-image->filename]='\0';
-          (void) ConcatenateMagickString(url,image->filename,(size_t)
-            (p-image->filename+2));
+          /*
+            Append the directory part of the filename (up to and including
+            the last '/'), bounded by the size of url.  The previous code
+            indexed url[strlen(url)+(p-filename)], which can land past the
+            end of the stack buffer for long filenames.
+          */
+          extent=strlen(url);
+          prefix=(size_t) (p-image->filename);
+          if (prefix > (MagickPathExtent-extent-1))
+            prefix=MagickPathExtent-extent-1;
+          (void) memcpy(url+extent,image->filename,prefix);
+          url[extent+prefix]='\0';
           (void) CopyMagickString(image->filename,p,MagickPathExtent);
         }
     }
@@ -322,10 +339,12 @@ static MagickBooleanType WriteHTMLImage(const ImageInfo *image_info,
       /*
         Open output image file.
       */
-      assert(exception != (ExceptionInfo *) NULL);
       status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       if (status == MagickFalse)
-        return(status);
+        {
+          write_info=DestroyImageInfo(write_info);
+          return(status);
+        }
       /*
         Write the HTML image file.
       */
@@ -398,7 +417,6 @@ static MagickBooleanType WriteHTMLImage(const ImageInfo *image_info,
                 {
                   (void) WriteBlobString(image,"  <area href=\"");
                   WriteHtmlEncodedString(image,url);
-                  (void) WriteBlobString(image,"\"");
                 }
               geometry.x+=(ssize_t) geometry.width;
               if ((geometry.x+4) >= (ssize_t) image->columns)
@@ -419,8 +437,13 @@ static MagickBooleanType WriteHTMLImage(const ImageInfo *image_info,
       next=GetNextImageInList(image);
       image->next=NewImageList();
       (void) CopyMagickString(image->magick,"PNG",MagickPathExtent);
-      (void) WriteImage(write_info,image,exception);
+      status=WriteImage(write_info,image,exception);
       image->next=next;
+      if (status == MagickFalse)
+        {
+          write_info=DestroyImageInfo(write_info);
+          return(status);
+        }
       /*
         Determine image map filename.
       */
@@ -432,9 +455,9 @@ static MagickBooleanType WriteHTMLImage(const ImageInfo *image_info,
     Open image map.
   */
   status=OpenBlob(write_info,image,WriteBinaryBlobMode,exception);
+  write_info=DestroyImageInfo(write_info);
   if (status == MagickFalse)
     return(status);
-  write_info=DestroyImageInfo(write_info);
   /*
     Determine the size and location of each image tile.
   */
@@ -472,9 +495,8 @@ static MagickBooleanType WriteHTMLImage(const ImageInfo *image_info,
           (void) WriteBlobString(image,buffer);
           if (*(p+1) != '\0')
             {
-              (void) FormatLocaleString(buffer,MagickPathExtent,
-                "  <area href=%s\"",url);
-              (void) WriteBlobString(image,buffer);
+              (void) WriteBlobString(image,"  <area href=\"");
+              WriteHtmlEncodedString(image,url);
             }
           geometry.x+=(ssize_t) geometry.width;
           if ((geometry.x+4) >= (ssize_t) image->columns)