Commit ac725a3ee for imagemagick.org

commit ac725a3eee6db07cc8dadba2efecdf08706e4edb
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat Dec 5 09:07:37 2026 -0500

    GetFillColor() and GetStrokeColor() now fall back to the solid fill or stroke color

diff --git a/MagickCore/draw-private.h b/MagickCore/draw-private.h
index 788a97d94..fcaa7491b 100644
--- a/MagickCore/draw-private.h
+++ b/MagickCore/draw-private.h
@@ -47,7 +47,14 @@ static inline void GetFillColor(const DrawInfo *draw_info,const ssize_t x,
   if (GetOneVirtualPixelInfo(pattern,TileVirtualPixelMethod,x+
         pattern->tile_offset.x,y+pattern->tile_offset.y,fill,
         exception) == MagickFalse)
-    return;
+    {
+      /*
+        Pattern lookup failed: fall back to the solid fill color rather
+        than painting whatever GetOneVirtualPixelInfo() left behind.
+      */
+      *fill=draw_info->fill;
+      return;
+    }
   if (fabs(draw_info->fill_alpha-(double) TransparentAlpha) >= MagickEpsilon)
     fill->alpha*=QuantumScale*draw_info->fill_alpha;
 }
@@ -69,7 +76,14 @@ static inline void GetStrokeColor(const DrawInfo *draw_info,const ssize_t x,
   if (GetOneVirtualPixelInfo(pattern,TileVirtualPixelMethod,x+
         pattern->tile_offset.x,y+pattern->tile_offset.y,stroke,
         exception) == MagickFalse)
-    return;
+    {
+      /*
+        Pattern lookup failed: fall back to the solid stroke color rather
+        than painting whatever GetOneVirtualPixelInfo() left behind.
+      */
+      *stroke=draw_info->stroke;
+      return;
+    }
   if (fabs(draw_info->stroke_alpha-(double) TransparentAlpha) >= MagickEpsilon)
     stroke->alpha*=QuantumScale*draw_info->stroke_alpha;
 }
diff --git a/MagickCore/draw.c b/MagickCore/draw.c
index 01b414303..cb227a023 100644
--- a/MagickCore/draw.c
+++ b/MagickCore/draw.c
@@ -1812,10 +1812,10 @@ static Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
       composite_mask=DestroyImage(composite_mask);
       composite_mask=separate_mask;
       status=NegateImage(composite_mask,MagickFalse,exception);
-      if (status == MagickFalse)
-        composite_mask=DestroyImage(composite_mask);
     }
-  if (status == MagickFalse)
+  else
+    status=MagickFalse;
+  if ((status == MagickFalse) && (composite_mask != (Image *) NULL))
     composite_mask=DestroyImage(composite_mask);
   if (draw_info->debug != MagickFalse)
     (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end mask-path");
@@ -3700,17 +3700,30 @@ static MagickBooleanType RenderMVGContent(Image *image,
               }
             if (LocaleCompare("graphic-context",token) == 0)
               {
-                n++;
-                graphic_context=(DrawInfo **) ResizeQuantumMemory(
-                  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
-                if (graphic_context == (DrawInfo **) NULL)
-                  {
-                    (void) ThrowMagickException(exception,GetMagickModule(),
-                      ResourceLimitError,"MemoryAllocationFailed","`%s'",
-                      image->filename);
-                    status=MagickFalse;
-                    break;
-                  }
+                {
+                  DrawInfo
+                    **contexts;
+
+                  /*
+                    Grow the context stack without losing it on failure.
+                  */
+                  contexts=(DrawInfo **) AcquireQuantumMemory((size_t) (n+2),
+                    sizeof(*graphic_context));
+                  if (contexts == (DrawInfo **) NULL)
+                    {
+                      (void) ThrowMagickException(exception,GetMagickModule(),
+                        ResourceLimitError,"MemoryAllocationFailed","`%s'",
+                        image->filename);
+                      status=MagickFalse;
+                      break;
+                    }
+                  (void) memcpy(contexts,graphic_context,(size_t) (n+1)*
+                    sizeof(*graphic_context));
+                  graphic_context=(DrawInfo **)
+                    RelinquishMagickMemory(graphic_context);
+                  graphic_context=contexts;
+                  n++;
+                }
                 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
                   graphic_context[n-1]);
                 if (*q == '"')
@@ -3870,7 +3883,7 @@ static MagickBooleanType RenderMVGContent(Image *image,
             angle=GetDrawValue(token,&next_token);
             if (token == next_token)
               ThrowPointExpectedException(token,exception);
-            affine.ry=sin(DegreesToRadians(angle));
+            affine.ry=tan(DegreesToRadians(fmod(angle,360.0)));
             break;
           }
         if (LocaleCompare("skewY",keyword) == 0)
@@ -3879,7 +3892,7 @@ static MagickBooleanType RenderMVGContent(Image *image,
             angle=GetDrawValue(token,&next_token);
             if (token == next_token)
               ThrowPointExpectedException(token,exception);
-            affine.rx=(-tan(DegreesToRadians(angle)/2.0));
+            affine.rx=tan(DegreesToRadians(fmod(angle,360.0)));
             break;
           }
         if (LocaleCompare("stop-color",keyword) == 0)
@@ -4193,7 +4206,8 @@ static MagickBooleanType RenderMVGContent(Image *image,
                           DrawError,"NonconformingDrawingPrimitiveDefinition",
                           "`%s'",token);
                         clone_info=DestroyDrawInfo(clone_info);
-                        return(MagickFalse);
+                        status=MagickFalse;
+                        break;
                       }
                     (*draw_info->macro_expansion)++;
                   }
@@ -5022,13 +5036,13 @@ static PolygonInfo **AcquirePolygonTLS(const PrimitiveInfo *primitive_info,
   if (path_info == (PathInfo *) NULL)
     return(DestroyPolygonTLS(polygon_info));
   polygon_info[0]=ConvertPathToPolygon(path_info,exception);
+  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
   if (polygon_info[0] == (PolygonInfo *) NULL)
     {
       (void) ThrowMagickException(exception,GetMagickModule(),
         ResourceLimitError,"MemoryAllocationFailed","`%s'","");
       return(DestroyPolygonTLS(polygon_info));
     }
-  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
   return(polygon_info);
 }

@@ -5545,6 +5559,7 @@ static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
   const char
     *methods[] =
     {
+      "undefined",
       "point",
       "replace",
       "floodfill",
@@ -5566,6 +5581,12 @@ static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
     coordinates,
     y;

+  size_t
+    method;
+
+  method=(size_t) primitive_info->method;
+  if (method > 6)
+    method=6;
   x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
   y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
   switch (primitive_info->primitive)
@@ -5574,14 +5595,14 @@ static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
     {
       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
         "AlphaPrimitive %.17g,%.17g %s",(double) x,(double) y,
-        methods[primitive_info->method]);
+        methods[method]);
       return;
     }
     case ColorPrimitive:
     {
       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
         "ColorPrimitive %.17g,%.17g %s",(double) x,(double) y,
-        methods[primitive_info->method]);
+        methods[method]);
       return;
     }
     case ImagePrimitive:
@@ -5594,7 +5615,7 @@ static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
     {
       (void) LogMagickEvent(DrawEvent,GetMagickModule(),
         "PointPrimitive %.17g,%.17g %s",(double) x,(double) y,
-        methods[primitive_info->method]);
+        methods[method]);
       return;
     }
     case TextPrimitive:
diff --git a/MagickCore/draw.h b/MagickCore/draw.h
index bf3efd08f..adb529835 100644
--- a/MagickCore/draw.h
+++ b/MagickCore/draw.h
@@ -355,6 +355,12 @@ typedef struct _DrawInfo
   ImageInfo
     *image_info;

+  /*
+    Borrowed pointer to a shared render-expansion counter (use, mask,
+    clip-path and pattern renders).  CloneDrawInfo() shares it and
+    DestroyDrawInfo() never frees it; leave it NULL or point it at storage
+    that outlives every clone made during the draw.
+  */
   size_t
     *macro_expansion;
 } DrawInfo;
diff --git a/coders/mvg.c b/coders/mvg.c
index 703ffa969..1fd9a42a6 100644
--- a/coders/mvg.c
+++ b/coders/mvg.c
@@ -160,7 +160,8 @@ static Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
         primitive[MagickPathExtent];

       char
-        *p;
+        *p,
+        *q;

       SegmentInfo
         bounds;
@@ -175,10 +176,23 @@ static Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
           count;

         for (p=primitive; (*p == ' ') || (*p == '\t'); p++) ;
-        count=MagickSscanf(p,"viewbox %lf %lf %lf %lf",&bounds.x1,&bounds.y1,
+        /*
+          The renderer matches keywords case-insensitively and accepts
+          comma-separated values, so the size probe must too.
+        */
+        if ((LocaleNCompare(p,"viewbox",7) != 0) ||
+            ((p[7] != ' ') && (p[7] != '\t')))
+          continue;
+        for (q=p+7; *q != '\0'; q++)
+          if (*q == ',')
+            *q=' ';
+        count=MagickSscanf(p+7,"%lf %lf %lf %lf",&bounds.x1,&bounds.y1,
           &bounds.x2,&bounds.y2);
         if (count != 4)
           continue;
+        if ((isfinite(bounds.x1) == 0) || (isfinite(bounds.y1) == 0) ||
+            (isfinite(bounds.x2) == 0) || (isfinite(bounds.y2) == 0))
+          continue;
         image->columns=CastDoubleToSizeT(floor((bounds.x2-bounds.x1)+0.5));
         image->rows=CastDoubleToSizeT(floor((bounds.y2-bounds.y1)+0.5));
         break;
@@ -195,6 +209,15 @@ static Image *ReadMVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
     96.0;
   image->columns=CastDoubleToSizeT(draw_info->affine.sx*image->columns);
   image->rows=CastDoubleToSizeT(draw_info->affine.sy*image->rows);
+  if (image_info->ping != MagickFalse)
+    {
+      /*
+        Ping only needs the canvas geometry; skip the render.
+      */
+      draw_info=DestroyDrawInfo(draw_info);
+      (void) CloseBlob(image);
+      return(GetFirstImageInList(image));
+    }
   status=SetImageExtent(image,image->columns,image->rows,exception);
   if (status == MagickFalse)
     {
diff --git a/coders/svg.c b/coders/svg.c
index cd88304c2..e0b722030 100644
--- a/coders/svg.c
+++ b/coders/svg.c
@@ -1252,7 +1252,7 @@ static void SVGProcessStyleElement(SVGInfo *svg_info,const xmlChar *name,
               break;
             if (GetUserSpaceCoordinateValue(svg_info,0,font_style) == 0)
               (void) FormatLocaleFile(svg_info->file,"font-style \"%s\"\n",
-                style);
+                font_style);
             else
               if (MagickSscanf(value,"%2048s %2048s",font_size,font_family) != 2)
                 break;
@@ -1347,7 +1347,7 @@ static void SVGProcessStyleElement(SVGInfo *svg_info,const xmlChar *name,
         if (LocaleCompare(keyword,"shape-rendering") == 0)
           {
             (void) FormatLocaleFile(svg_info->file,"stroke-antialias %d\n",
-              LocaleCompare(value,"crispEdges") == 0);
+              LocaleCompare(value,"crispEdges") != 0);
             break;
           }
         if (LocaleCompare(keyword,"stop-color") == 0)
@@ -1363,7 +1363,7 @@ static void SVGProcessStyleElement(SVGInfo *svg_info,const xmlChar *name,
                 break;
               }
             if (LocaleCompare(value,"#000000ff") == 0)
-              (void) FormatLocaleFile(svg_info->file,"fill '#000000'\n");
+              (void) FormatLocaleFile(svg_info->file,"stroke '#000000'\n");
             else
               (void) FormatLocaleFile(svg_info->file,
                 "stroke \"%s\"\n",value);
@@ -1519,6 +1519,7 @@ static void SVGStartElement(void *context,const xmlChar *name,
     {
       (void) ThrowMagickException(svg_info->exception,GetMagickModule(),
         ResourceLimitError,"MemoryAllocationFailed","`%s'",name);
+      xmlStopParser((xmlParserCtxtPtr) context);
       return;
     }
   svg_info->scale[svg_info->n]=svg_info->scale[svg_info->n-1];
@@ -2388,7 +2389,7 @@ static void SVGStartElement(void *context,const xmlChar *name,
           if (LocaleCompare(keyword,"shape-rendering") == 0)
             {
               (void) FormatLocaleFile(svg_info->file,"stroke-antialias %d\n",
-                LocaleCompare(value,"crispEdges") == 0);
+                LocaleCompare(value,"crispEdges") != 0);
               break;
             }
           if (LocaleCompare(keyword,"stop-color") == 0)
@@ -3110,8 +3111,19 @@ static void SVGEndElement(void *context,const xmlChar *name)
           {
             keyword=(char *) tokens[j];
             value=(char *) tokens[j+1];
-            (void) FormatLocaleFile(svg_info->file,"push class \"%s\"\n",
-              *keyword == '.' ? keyword+1 : keyword);
+            {
+              char
+                *escaped_class;
+
+              /*
+                CSS selectors are raw document text: escape before quoting.
+              */
+              escaped_class=SVGEscapeString(*keyword == '.' ? keyword+1 :
+                keyword);
+              (void) FormatLocaleFile(svg_info->file,"push class \"%s\"\n",
+                escaped_class);
+              escaped_class=DestroyString(escaped_class);
+            }
             SVGProcessStyleElement(svg_info,name,value);
             (void) FormatLocaleFile(svg_info->file,"pop class\n");
           }
@@ -3187,8 +3199,9 @@ static void SVGEndElement(void *context,const xmlChar *name)
           if ((svg_info->bounds.x != 0.0) || (svg_info->bounds.y != 0.0))
             (void) FormatLocaleFile(svg_info->file,"translate %g,%g\n",
               svg_info->bounds.x,svg_info->bounds.y);
-          (void) FormatLocaleFile(svg_info->file,"use \"url(%s)\"\n",
-            svg_info->url);
+          if (svg_info->url != (char *) NULL)
+            (void) FormatLocaleFile(svg_info->file,"use \"url(%s)\"\n",
+              svg_info->url);
           (void) FormatLocaleFile(svg_info->file,"pop graphic-context\n");
           break;
         }
@@ -3394,6 +3407,7 @@ static Image *RenderMSVGImage(const ImageInfo *image_info,Image *image,
   if (svg_info == (SVGInfo *) NULL)
     {
       (void) fclose(file);
+      (void) RelinquishUniqueFileResource(filename);
       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
     }
   svg_info->file=file;
@@ -3463,6 +3477,7 @@ static Image *RenderMSVGImage(const ImageInfo *image_info,Image *image,
   if (parser == (xmlParserCtxtPtr) NULL)
     {
       svg_info=DestroySVGInfo(svg_info);
+      (void) fclose(file);
       (void) RelinquishUniqueFileResource(filename);
       image=DestroyImage(image);
       return((Image *) NULL);
@@ -3477,7 +3492,12 @@ static Image *RenderMSVGImage(const ImageInfo *image_info,Image *image,
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"end SAX");
   if (fseek(file,0,SEEK_SET) != 0)
-    ThrowReaderException(FileOpenError,"UnableToCreateTemporaryFile");
+    {
+      svg_info=DestroySVGInfo(svg_info);
+      (void) fclose(file);
+      (void) RelinquishUniqueFileResource(filename);
+      ThrowReaderException(FileOpenError,"UnableToCreateTemporaryFile");
+    }
   (void) CloseBlob(image);
   image->columns=svg_info->width;
   image->rows=svg_info->height;
@@ -3508,6 +3528,7 @@ static Image *RenderMSVGImage(const ImageInfo *image_info,Image *image,
         filename);
       image=ReadImageWithoutPostProcessing(read_info,exception);
       read_info=DestroyImageInfo(read_info);
+      (void) fclose(file);  /* the MVG reader treats a caller FILE as exempt */
       if (image != (Image *) NULL)
         (void) CopyMagickString(image->filename,image_info->filename,
           MagickPathExtent);
@@ -4175,6 +4196,8 @@ static MagickBooleanType WriteSVGImage(const ImageInfo *image_info,Image *image,
   if (primitive_info == (PrimitiveInfo *) NULL)
     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
   GetAffineMatrix(&affine);
+  *name='\0';
+  *type='\0';
   token=AcquireString(value);
   extent=strlen(token)+MagickPathExtent;
   active=MagickFalse;