Commit b82ec6963 for imagemagick.org

commit b82ec6963746035d06e499f3a5431216158feb4e
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Mon Nov 30 21:55:46 2026 -0500

    the stride was cast to unsigned without checking for the -1 error value

diff --git a/coders/pango.c b/coders/pango.c
index c2141bfce..de28ad4c8 100644
--- a/coders/pango.c
+++ b/coders/pango.c
@@ -167,6 +167,10 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
   RectangleInfo
     page;

+  ssize_t
+    height,
+    width;
+
   unsigned char
     *p;

@@ -212,7 +216,12 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
       (void) SetImageProperty(image,"caption",property,exception);
       property=DestroyString(property);
     }
-  caption=ConstantString(GetImageProperty(image,"caption",exception));
+  option=GetImageProperty(image,"caption",exception);
+  if (option == (const char *) NULL)
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  caption=ConstantString(option);
+  if (caption == (char *) NULL)
+    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
   /*
     Get context.
   */
@@ -223,9 +232,9 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
   option=GetImageOption(image_info,"pango:hinting");
   if (option != (const char *) NULL)
     {
-      if (LocaleCompare(option,"none") != 0)
+      if (LocaleCompare(option,"none") == 0)
         cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_NONE);
-      if (LocaleCompare(option,"full") != 0)
+      if (LocaleCompare(option,"full") == 0)
         cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_FULL);
     }
   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
@@ -286,9 +295,11 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
     Configure layout.
   */
   layout=pango_layout_new(context);
+  g_object_unref(context);
   option=GetImageOption(image_info,"pango:auto-dir");
   if (option != (const char *) NULL)
-    pango_layout_set_auto_dir(layout,1);
+    pango_layout_set_auto_dir(layout,IsStringTrue(option) != MagickFalse ? 1 :
+      0);
   option=GetImageOption(image_info,"pango:ellipsize");
   if (option != (const char *) NULL)
     {
@@ -369,16 +380,22 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,

       error=(GError *) NULL;
       if (pango_parse_markup(caption,-1,0,NULL,NULL,NULL,&error) == 0)
-        (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
-          error->message,"`%s'",image_info->filename);
-      pango_layout_set_markup(layout,caption,-1);
+        {
+          (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
+            (error != (GError *) NULL) && (error->message != (gchar *) NULL) ?
+            error->message : "InvalidPangoMarkup","`%s'",image_info->filename);
+          if (error != (GError *) NULL)
+            g_error_free(error);
+          pango_layout_set_text(layout,caption,-1);
+        }
+      else
+        pango_layout_set_markup(layout,caption,-1);
     }
   if (draw_info->interline_spacing != 0)
     pango_layout_set_spacing(layout,ScalePangoValue(
       draw_info->interline_spacing,image->resolution.x));
   pango_layout_context_changed(layout);
-  page.x=0;
-  page.y=0;
+  (void) memset(&page,0,sizeof(page));
   if (image_info->page != (char *) NULL)
     (void) ParseAbsoluteGeometry(image_info->page,&page);
   if (image->columns == 0)
@@ -389,33 +406,76 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
     }
   else
     {
-      image->columns=(size_t) ((ssize_t) image->columns-2*page.x);
+      width=(ssize_t) image->columns-2*page.x;
+      if (width <= 0)
+        {
+          g_object_unref(layout);
+          g_object_unref(fontmap);
+          draw_info=DestroyDrawInfo(draw_info);
+          caption=DestroyString(caption);
+          ThrowReaderException(OptionError,"InvalidGeometry");
+        }
+      image->columns=(size_t) width;
       pango_layout_set_width(layout,ScalePangoValue((double) image->columns,
         image->resolution.x));
     }
   if (image->rows == 0)
-    {
+    {
       pango_layout_get_extents(layout,NULL,&extent);
       image->rows=(size_t) ((extent.y+extent.height+PANGO_SCALE/2)/
         PANGO_SCALE+2*page.y);
     }
   else
     {
-      image->rows=(size_t) ((ssize_t) image->rows-2*page.y);
+      height=(ssize_t) image->rows-2*page.y;
+      if (height <= 0)
+        {
+          g_object_unref(layout);
+          g_object_unref(fontmap);
+          draw_info=DestroyDrawInfo(draw_info);
+          caption=DestroyString(caption);
+          ThrowReaderException(OptionError,"InvalidGeometry");
+        }
+      image->rows=(size_t) height;
       pango_layout_set_height(layout,ScalePangoValue((double) image->rows,
         image->resolution.y));
     }
   status=SetImageExtent(image,image->columns,image->rows,exception);
   if (status == MagickFalse)
-    return(DestroyImageList(image));
+    {
+      g_object_unref(layout);
+      g_object_unref(fontmap);
+      draw_info=DestroyDrawInfo(draw_info);
+      caption=DestroyString(caption);
+      return(DestroyImageList(image));
+    }
   /*
     Render markup.
   */
-  stride=(size_t) cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
+  if ((image->columns > (size_t) INT_MAX) || (image->rows > (size_t) INT_MAX))
+    {
+      g_object_unref(layout);
+      g_object_unref(fontmap);
+      draw_info=DestroyDrawInfo(draw_info);
+      caption=DestroyString(caption);
+      ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
+    }
+  width=(ssize_t) cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
     (int) image->columns);
+  if (width <= 0)
+    {
+      g_object_unref(layout);
+      g_object_unref(fontmap);
+      draw_info=DestroyDrawInfo(draw_info);
+      caption=DestroyString(caption);
+      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+    }
+  stride=(size_t) width;
   pixel_info=AcquireVirtualMemory(image->rows,stride*sizeof(*pixels));
   if (pixel_info == (MemoryInfo *) NULL)
     {
+      g_object_unref(layout);
+      g_object_unref(fontmap);
       draw_info=DestroyDrawInfo(draw_info);
       caption=DestroyString(caption);
       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
@@ -423,6 +483,16 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
   pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
   surface=cairo_image_surface_create_for_data(pixels,CAIRO_FORMAT_ARGB32,
     (int) image->columns,(int) image->rows,(int) stride);
+  if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
+    {
+      cairo_surface_destroy(surface);
+      g_object_unref(layout);
+      g_object_unref(fontmap);
+      pixel_info=RelinquishVirtualMemory(pixel_info);
+      draw_info=DestroyDrawInfo(draw_info);
+      caption=DestroyString(caption);
+      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+    }
   cairo_image=cairo_create(surface);
   cairo_set_operator(cairo_image,CAIRO_OPERATOR_CLEAR);
   cairo_paint(cairo_image);
@@ -432,6 +502,7 @@ static Image *ReadPANGOImage(const ImageInfo *image_info,
     QuantumScale*draw_info->fill.green,QuantumScale*draw_info->fill.blue,
     QuantumScale*draw_info->fill.alpha);
   pango_cairo_show_layout(cairo_image,layout);
+  cairo_surface_flush(surface);
   cairo_destroy(cairo_image);
   cairo_surface_destroy(surface);
   g_object_unref(layout);