From 3d6d98d8a2be30d74172ab43b5b8e874d2deb158 Mon Sep 17 00:00:00 2001
From: Cristy <urban-warrior@imagemagick.org>
Date: Wed, 17 May 2023 21:06:15 -0400
Subject: [PATCH] properly cast double to size_t
 (https://github.com/ImageMagick/ImageMagick/issues/6341)

Link: https://github.com/ImageMagick/ImageMagick/commit/3d6d98d8a2be30d74172ab43b5b8e874d2deb158 

---
 MagickCore/annotate.c       |  4 ++--
 MagickCore/draw.c           |  2 +-
 MagickCore/geometry.c       |  6 +++---
 MagickCore/shear.c          |  6 +++---
 MagickCore/visual-effects.c |  4 ++--
 coders/caption.c            | 10 +++++-----
 coders/label.c              | 10 +++++-----
 coders/pcl.c                |  4 ++--
 coders/pdf.c                |  4 ++--
 coders/ps.c                 |  4 ++--
 coders/ps2.c                |  4 ++--
 coders/ps3.c                |  4 ++--
 coders/svg.c                |  4 ++--
 13 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/MagickCore/annotate.c b/MagickCore/annotate.c
index bd2da50f36..b635d36bfb 100644
--- a/MagickCore/annotate.c
+++ b/MagickCore/annotate.c
@@ -341,7 +341,7 @@ MagickExport MagickBooleanType AnnotateImage(Image *image,
     (void) CloneString(&annotate->text,textlist[i]);
     if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
       (void) GetTypeMetrics(image,annotate,&metrics,exception);
-    height=(size_t) floor(metrics.ascent-metrics.descent+0.5);
+    height=CastDoubleToUnsigned(metrics.ascent-metrics.descent+0.5);
     if (height == 0)
       height=draw_info->pointsize;
     height+=(size_t) floor(draw_info->interline_spacing+0.5);
@@ -673,7 +673,7 @@ MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
     status=GetTypeMetrics(image,draw_info,metrics,exception);
     if (status == MagickFalse)
       break;
-    width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
+    width=CastDoubleToUnsigned(metrics->width+draw_info->stroke_width+0.5);
     if (width <= image->columns)
       continue;
     if (s != (char *) NULL)
diff --git a/MagickCore/draw.c b/MagickCore/draw.c
index 3d46ba2cb3..d88729d9da 100644
--- a/MagickCore/draw.c
+++ b/MagickCore/draw.c
@@ -3515,7 +3515,7 @@ static MagickBooleanType RenderMVGContent(Image *image,
                 (void) GetNextToken(q,&q,extent,token);
                 if (*token == ',')
                   (void) GetNextToken(q,&q,extent,token);
-                region.height=(size_t) floor(GetDrawValue(token,&next_token)+
+                region.height=CastDoubleToUnsigned(GetDrawValue(token,&next_token)+
                   0.5);
                 if (token == next_token)
                   ThrowPointExpectedException(token,exception);
diff --git a/MagickCore/geometry.c b/MagickCore/geometry.c
index 977183b576..4d201f36d6 100644
--- a/MagickCore/geometry.c
+++ b/MagickCore/geometry.c
@@ -1515,8 +1515,8 @@ MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
       scale.y=geometry_info.sigma;
       if ((percent_flags & SigmaValue) == 0)
         scale.y=scale.x;
-      *width=(size_t) floor(scale.x*stasis_width/100.0+0.5);
-      *height=(size_t) floor(scale.y*stasis_height/100.0+0.5);
+      *width=CastDoubleToUnsigned(scale.x*stasis_width/100.0+0.5);
+      *height=CastDoubleToUnsigned(scale.y*stasis_height/100.0+0.5);
       stasis_width=(*width);
       stasis_height=(*height);
     }
@@ -1536,7 +1536,7 @@ MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
       if (geometry_ratio >= image_ratio)
         {
           *width=stasis_width;
-          *height=(size_t) floor((double) (PerceptibleReciprocal(
+          *height=CastDoubleToUnsigned((double) (PerceptibleReciprocal(
             geometry_ratio)*stasis_height*image_ratio)+0.5);
         }
       else
diff --git a/MagickCore/shear.c b/MagickCore/shear.c
index 5cfa7be965..04e785ea6c 100644
--- a/MagickCore/shear.c
+++ b/MagickCore/shear.c
@@ -1768,9 +1768,9 @@ MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
   */
   width=integral_image->columns;
   height=integral_image->rows;
-  bounds.width=(size_t) floor(fabs((double) height*shear.x)+width+0.5);
-  bounds.height=(size_t) floor(fabs((double) bounds.width*shear.y)+height+0.5);
-  shear_width=(size_t) floor(fabs((double) bounds.height*shear.x)+
+  bounds.width=CastDoubleToUnsigned(fabs((double) height*shear.x)+width+0.5);
+  bounds.height=CastDoubleToUnsigned(fabs((double) bounds.width*shear.y)+height+0.5);
+  shear_width=CastDoubleToUnsigned(fabs((double) bounds.height*shear.x)+
     bounds.width+0.5);
   bounds.x=CastDoubleToLong(floor((double) ((shear_width > bounds.width) ?
     width : bounds.width-shear_width+2)/2.0+0.5));
diff --git a/MagickCore/visual-effects.c b/MagickCore/visual-effects.c
index 80024212e8..5257865ee3 100644
--- a/MagickCore/visual-effects.c
+++ b/MagickCore/visual-effects.c
@@ -2060,8 +2060,8 @@ MagickExport Image *ShadowImage(const Image *image,const double alpha,
     (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
   (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
     exception);
-  border_info.width=(size_t) floor(2.0*sigma+0.5);
-  border_info.height=(size_t) floor(2.0*sigma+0.5);
+  border_info.width=CastDoubleToUnsigned(2.0*sigma+0.5);
+  border_info.height=CastDoubleToUnsigned(2.0*sigma+0.5);
   border_info.x=0;
   border_info.y=0;
   (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
diff --git a/coders/caption.c b/coders/caption.c
index 81aeb15830..35f551b31d 100644
--- a/coders/caption.c
+++ b/coders/caption.c
@@ -169,7 +169,7 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
     return(DestroyImageList(image));
   (void) SetImageProperty(image,"caption",caption,exception);
   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
-  width=(size_t) floor(0.5*draw_info->pointsize*strlen(caption)+0.5);
+  width=CastDoubleToUnsigned(0.5*draw_info->pointsize*strlen(caption)+0.5);
   if (AcquireMagickResource(WidthResource,width) == MagickFalse)
     {
       caption=DestroyString(caption);
@@ -277,8 +277,8 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
             if (status == MagickFalse)
               break;
             AdjustTypeMetricBounds(&metrics);
-            width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
-            height=(size_t) floor(metrics.height-metrics.underline_position+
+            width=CastDoubleToUnsigned(metrics.width+draw_info->stroke_width+0.5);
+            height=CastDoubleToUnsigned(metrics.height-metrics.underline_position+
               draw_info->interline_spacing+draw_info->stroke_width+0.5);
             if ((image->columns != 0) && (image->rows != 0))
               {
@@ -310,8 +310,8 @@ static Image *ReadCAPTIONImage(const ImageInfo *image_info,
         if (status == MagickFalse)
           break;
         AdjustTypeMetricBounds(&metrics);
-        width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
-        height=(size_t) floor(metrics.height-metrics.underline_position+
+        width=CastDoubleToUnsigned(metrics.width+draw_info->stroke_width+0.5);
+        height=CastDoubleToUnsigned(metrics.height-metrics.underline_position+
           draw_info->interline_spacing+draw_info->stroke_width+0.5);
         if ((image->columns != 0) && (image->rows != 0))
           {
diff --git a/coders/label.c b/coders/label.c
index 1ec2508f60..5d08035a25 100644
--- a/coders/label.c
+++ b/coders/label.c
@@ -151,7 +151,7 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
     return(DestroyImageList(image));
   (void) SetImageProperty(image,"label",label,exception);
   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
-  width=(size_t) floor(0.5*draw_info->pointsize*strlen(label)+0.5);
+  width=CastDoubleToUnsigned(0.5*draw_info->pointsize*strlen(label)+0.5);
   if (AcquireMagickResource(WidthResource,width) == MagickFalse)
     {
       label=DestroyString(label);
@@ -214,8 +214,8 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
               if (status == MagickFalse)
                 break;
               AdjustTypeMetricBounds(&metrics);
-              width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
-              height=(size_t) floor(metrics.height-metrics.underline_position+
+              width=CastDoubleToUnsigned(metrics.width+draw_info->stroke_width+0.5);
+              height=CastDoubleToUnsigned(metrics.height-metrics.underline_position+
                 draw_info->stroke_width+0.5);
               if ((image->columns != 0) && (image->rows != 0))
                 {
@@ -249,8 +249,8 @@ static Image *ReadLABELImage(const ImageInfo *image_info,
           if (status == MagickFalse)
             break;
           AdjustTypeMetricBounds(&metrics);
-          width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
-          height=(size_t) floor(metrics.height-metrics.underline_position+
+          width=CastDoubleToUnsigned(metrics.width+draw_info->stroke_width+0.5);
+          height=CastDoubleToUnsigned(metrics.height-metrics.underline_position+
             draw_info->stroke_width+0.5);
           if ((image->columns != 0) && (image->rows != 0))
             {
diff --git a/coders/pcl.c b/coders/pcl.c
index b5f6818bd3..0dae2772fa 100644
--- a/coders/pcl.c
+++ b/coders/pcl.c
@@ -334,8 +334,8 @@ static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
     image->resolution.x,image->resolution.y);
   if (image_info->ping != MagickFalse)
     (void) FormatLocaleString(density,MagickPathExtent,"2.0x2.0");
-  page.width=(size_t) floor(page.width*image->resolution.x/delta.x+0.5);
-  page.height=(size_t) floor(page.height*image->resolution.y/delta.y+0.5);
+  page.width=CastDoubleToUnsigned(page.width*image->resolution.x/delta.x+0.5);
+  page.height=CastDoubleToUnsigned(page.height*image->resolution.y/delta.y+0.5);
   (void) FormatLocaleString(options,MagickPathExtent,"-g%.20gx%.20g ",(double)
     page.width,(double) page.height);
   image=DestroyImage(image);
diff --git a/coders/pdf.c b/coders/pdf.c
index 2cf36bf1e9..5ba15aee2f 100644
--- a/coders/pdf.c
+++ b/coders/pdf.c
@@ -1867,9 +1867,9 @@ static MagickBooleanType WritePDFImage(const ImageInfo *image_info,Image *image,
     (void) ParseMetaGeometry(temp,&geometry.x,&geometry.y,
       &geometry.width,&geometry.height);
     scale.x=(double) (geometry.width*delta.x)/resolution.x;
-    geometry.width=(size_t) floor(scale.x+0.5);
+    geometry.width=CastDoubleToUnsigned(scale.x+0.5);
     scale.y=(double) (geometry.height*delta.y)/resolution.y;
-    geometry.height=(size_t) floor(scale.y+0.5);
+    geometry.height=CastDoubleToUnsigned(scale.y+0.5);
     (void) ParseAbsoluteGeometry(temp,&media_info);
     (void) ParseGravityGeometry(image,temp,&page_info,exception);
     if (image->gravity != UndefinedGravity)
diff --git a/coders/ps.c b/coders/ps.c
index ce2bb91765..47e48f1f38 100644
--- a/coders/ps.c
+++ b/coders/ps.c
@@ -1527,9 +1527,9 @@ static MagickBooleanType WritePSImage(const ImageInfo *image_info,Image *image,
     (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
       &geometry.width,&geometry.height);
     scale.x=PerceptibleReciprocal(resolution.x)*geometry.width*delta.x;
-    geometry.width=(size_t) floor(scale.x+0.5);
+    geometry.width=CastDoubleToUnsigned(scale.x+0.5);
     scale.y=PerceptibleReciprocal(resolution.y)*geometry.height*delta.y;
-    geometry.height=(size_t) floor(scale.y+0.5);
+    geometry.height=CastDoubleToUnsigned(scale.y+0.5);
     (void) ParseAbsoluteGeometry(page_geometry,&media_info);
     (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
     if (image->gravity != UndefinedGravity)
diff --git a/coders/ps2.c b/coders/ps2.c
index 766874dc02..eb2d7cbda2 100644
--- a/coders/ps2.c
+++ b/coders/ps2.c
@@ -533,9 +533,9 @@ static MagickBooleanType WritePS2Image(const ImageInfo *image_info,Image *image,
     (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
       &geometry.width,&geometry.height);
     scale.x=PerceptibleReciprocal(resolution.x)*geometry.width*delta.x;
-    geometry.width=(size_t) floor(scale.x+0.5);
+    geometry.width=CastDoubleToUnsigned(scale.x+0.5);
     scale.y=PerceptibleReciprocal(resolution.y)*geometry.height*delta.y;
-    geometry.height=(size_t) floor(scale.y+0.5);
+    geometry.height=CastDoubleToUnsigned(scale.y+0.5);
     (void) ParseAbsoluteGeometry(page_geometry,&media_info);
     (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
     if (image->gravity != UndefinedGravity)
diff --git a/coders/ps3.c b/coders/ps3.c
index b75787bd02..fd547fff41 100644
--- a/coders/ps3.c
+++ b/coders/ps3.c
@@ -985,9 +985,9 @@ static MagickBooleanType WritePS3Image(const ImageInfo *image_info,Image *image,
     (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
       &geometry.width,&geometry.height);
     scale.x=PerceptibleReciprocal(resolution.x)*geometry.width*delta.x;
-    geometry.width=(size_t) floor(scale.x+0.5);
+    geometry.width=CastDoubleToUnsigned(scale.x+0.5);
     scale.y=PerceptibleReciprocal(resolution.y)*geometry.height*delta.y;
-    geometry.height=(size_t) floor(scale.y+0.5);
+    geometry.height=CastDoubleToUnsigned(scale.y+0.5);
     (void) ParseAbsoluteGeometry(page_geometry,&media_info);
     (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
     if (image->gravity != UndefinedGravity)
diff --git a/coders/svg.c b/coders/svg.c
index 5a1e61c76a..1155133390 100644
--- a/coders/svg.c
+++ b/coders/svg.c
@@ -2826,10 +2826,10 @@ static void SVGStartElement(void *context,const xmlChar *name,
             svg_info->view_box=svg_info->bounds;
           svg_info->width=0;
           if (svg_info->bounds.width >= MagickEpsilon)
-            svg_info->width=(size_t) floor(svg_info->bounds.width+0.5);
+            svg_info->width=CastDoubleToUnsigned(svg_info->bounds.width+0.5);
           svg_info->height=0;
           if (svg_info->bounds.height >= MagickEpsilon)
-            svg_info->height=(size_t) floor(svg_info->bounds.height+0.5);
+            svg_info->height=CastDoubleToUnsigned(svg_info->bounds.height+0.5);
           (void) FormatLocaleFile(svg_info->file,"viewbox 0 0 %.20g %.20g\n",
             (double) svg_info->width,(double) svg_info->height);
           sx=PerceptibleReciprocal(svg_info->view_box.width)*svg_info->width;
