Commit 81f3c4306 for imagemagick.org

commit 81f3c43062b6ed9fcdd8025511819dd7efed3bd9
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat Dec 5 11:32:12 2026 -0500

    ModifyImage() returns fail when CloneImage fails + image-views guard against huge views

diff --git a/MagickCore/image-private.h b/MagickCore/image-private.h
index 7f92fd2e9..64607fb25 100644
--- a/MagickCore/image-private.h
+++ b/MagickCore/image-private.h
@@ -269,6 +269,8 @@ static inline MagickBooleanType IsValidFormatSpecifier(const char *start,
   length=(size_t) (end-start);
   if ((specifier != 'd') && (specifier != 'x') && (specifier != 'o'))
     return(MagickFalse);
+  if (length > 8)
+    return(MagickFalse);
   if ((length == 1) && (*start == specifier))
     return(MagickTrue);
   if (length >= 2)
diff --git a/MagickCore/image-view.c b/MagickCore/image-view.c
index 08e64a81f..ca99760f8 100644
--- a/MagickCore/image-view.c
+++ b/MagickCore/image-view.c
@@ -78,6 +78,26 @@ struct _ImageView
   size_t
     signature;
 };
+
+/*
+  Reject an extent whose height or width cannot be represented as a ssize_t
+  or whose y+height would overflow, so the iterator loop bound
+  (extent.y+extent.height) is always well defined.
+*/
+static MagickBooleanType IsImageViewExtentValid(const ImageView *image_view)
+{
+  if ((image_view->extent.width > (size_t) MAGICK_SSIZE_MAX) ||
+      (image_view->extent.height > (size_t) MAGICK_SSIZE_MAX) ||
+      ((image_view->extent.y > 0) && ((ssize_t) image_view->extent.height >
+       (MAGICK_SSIZE_MAX-image_view->extent.y))))
+    {
+      (void) ThrowMagickException(image_view->exception,GetMagickModule(),
+        OptionError,"GeometryDoesNotContainImage","`%s'",
+        image_view->image->filename);
+      return(MagickFalse);
+    }
+  return(MagickTrue);
+}

 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -237,6 +257,10 @@ MagickExport MagickBooleanType DuplexTransferImageViewIterator(
   assert(destination->signature == MagickCoreSignature);
   if (transfer == (DuplexTransferImageViewMethod) NULL)
     return(MagickFalse);
+  if ((IsImageViewExtentValid(source) == MagickFalse) ||
+      (IsImageViewExtentValid(duplex) == MagickFalse) ||
+      (IsImageViewExtentValid(destination) == MagickFalse))
+    return(MagickFalse);
   source_image=source->image;
   destination_image=destination->image;
   status=SetImageStorageClass(destination_image,DirectClass,
@@ -556,6 +580,8 @@ MagickExport MagickBooleanType GetImageViewIterator(ImageView *source,
   assert(source->signature == MagickCoreSignature);
   if (get == (GetImageViewMethod) NULL)
     return(MagickFalse);
+  if (IsImageViewExtentValid(source) == MagickFalse)
+    return(MagickFalse);
   source_image=source->image;
   status=MagickTrue;
   progress=0;
@@ -901,6 +927,8 @@ MagickExport MagickBooleanType SetImageViewIterator(ImageView *destination,
   assert(destination->signature == MagickCoreSignature);
   if (set == (SetImageViewMethod) NULL)
     return(MagickFalse);
+  if (IsImageViewExtentValid(destination) == MagickFalse)
+    return(MagickFalse);
   destination_image=destination->image;
   status=SetImageStorageClass(destination_image,DirectClass,
     destination->exception);
@@ -1032,6 +1060,9 @@ MagickExport MagickBooleanType TransferImageViewIterator(ImageView *source,
   assert(destination->signature == MagickCoreSignature);
   if (transfer == (TransferImageViewMethod) NULL)
     return(MagickFalse);
+  if ((IsImageViewExtentValid(source) == MagickFalse) ||
+      (IsImageViewExtentValid(destination) == MagickFalse))
+    return(MagickFalse);
   source_image=source->image;
   destination_image=destination->image;
   status=SetImageStorageClass(destination_image,DirectClass,
@@ -1167,6 +1198,8 @@ MagickExport MagickBooleanType UpdateImageViewIterator(ImageView *source,
   assert(source->signature == MagickCoreSignature);
   if (update == (UpdateImageViewMethod) NULL)
     return(MagickFalse);
+  if (IsImageViewExtentValid(source) == MagickFalse)
+    return(MagickFalse);
   source_image=source->image;
   status=SetImageStorageClass(source_image,DirectClass,source->exception);
   if (status == MagickFalse)
diff --git a/MagickCore/image.c b/MagickCore/image.c
index e3f68127b..eb00774c6 100644
--- a/MagickCore/image.c
+++ b/MagickCore/image.c
@@ -292,19 +292,16 @@ MagickExport Image *AcquireImage(const ImageInfo *image_info,
       if ((flags & GreaterValue) != 0)
         {
           if ((double) image->delay > floor(geometry_info.rho+0.5))
-            image->delay=(size_t) CastDoubleToSsizeT(floor(geometry_info.rho+
-              0.5));
+            image->delay=CastDoubleToSizeT(floor(geometry_info.rho+0.5));
         }
       else
         if ((flags & LessValue) != 0)
           {
             if ((double) image->delay < floor(geometry_info.rho+0.5))
-              image->ticks_per_second=CastDoubleToSsizeT(floor(
-                geometry_info.sigma+0.5));
+              image->delay=CastDoubleToSizeT(floor(geometry_info.rho+0.5));
           }
         else
-          image->delay=(size_t) CastDoubleToSsizeT(floor(geometry_info.rho+
-            0.5));
+          image->delay=CastDoubleToSizeT(floor(geometry_info.rho+0.5));
       if ((flags & SigmaValue) != 0)
         image->ticks_per_second=CastDoubleToSsizeT(floor(geometry_info.sigma+
           0.5));
@@ -748,7 +745,10 @@ MagickExport MagickBooleanType ClipImagePath(Image *image,const char *pathname,
     {
       (void) SyncImage(clip_mask,exception);
       if (SetImageStorageClass(clip_mask,DirectClass,exception) == MagickFalse)
-        return(MagickFalse);
+        {
+          clip_mask=DestroyImage(clip_mask);
+          return(MagickFalse);
+        }
     }
   if (inside != MagickFalse)
     (void) NegateImage(clip_mask,MagickFalse,exception);
@@ -919,7 +919,7 @@ MagickExport Image *CloneImage(const Image *image,const size_t columns,
     (double) image->page.x-0.5));
   clone_image->tile_offset.x=CastDoubleToSsizeT(ceil((double) scale_x*
     (double) image->tile_offset.x-0.5));
-  clone_image->page.y=CastDoubleToSsizeT(ceil((double) scale_y*(double)
+  clone_image->page.y=CastDoubleToSsizeT(ceil((double) scale_y*
     (double) image->page.y-0.5));
   clone_image->tile_offset.y=CastDoubleToSsizeT(ceil((double) scale_y*
     (double) image->tile_offset.y-0.5));
@@ -1162,7 +1162,8 @@ MagickExport MagickBooleanType CopyImagePixels(Image *image,
         #pragma omp atomic
 #endif
         progress++;
-        proceed=SetImageProgress(image,CopyImageTag,progress,image->rows);
+        proceed=SetImageProgress(image,CopyImageTag,progress,
+          geometry->height);
         if (proceed == MagickFalse)
           status=MagickFalse;
       }
@@ -1880,7 +1881,7 @@ MagickExport MagickBooleanType IsHighDynamicRangeImage(const Image *image,
         PixelTrait
           traits;

-        traits=GetPixelChannelTraits(image,(PixelChannel) i);
+        traits=GetPixelChannelTraits(image,GetPixelChannelChannel(image,i));
         if (traits == UndefinedPixelTrait)
           continue;
         pixel=(double) p[i];
@@ -2028,6 +2029,8 @@ MagickExport MagickBooleanType ModifyImage(Image **image,
   if (GetImageReferenceCount(*image) <= 1)
     return(MagickTrue);
   clone_image=CloneImage(*image,0,0,MagickTrue,exception);
+  if (clone_image == (Image *) NULL)
+    return(MagickFalse);  /* keep the caller's reference intact on failure */
   LockSemaphoreInfo((*image)->semaphore);
   (*image)->reference_count--;
   UnlockSemaphoreInfo((*image)->semaphore);
@@ -2090,12 +2093,16 @@ MagickExport Image *NewMagickImage(const ImageInfo *image_info,
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
   image=AcquireImage(image_info,exception);
-  image->columns=width;
-  image->rows=height;
   image->colorspace=background->colorspace;
   image->alpha_trait=background->alpha_trait;
   image->fuzz=background->fuzz;
   image->depth=background->depth;
+  /*
+    Validate the requested canvas (zero or oversized dimensions) before
+    touching any pixels.
+  */
+  if (SetImageExtent(image,width,height,exception) == MagickFalse)
+    return(DestroyImage(image));
   status=MagickTrue;
   image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
@@ -3817,12 +3824,12 @@ MagickExport Image *SmushImages(const Image *images,
           width=next->columns;
         height+=next->rows;
         if (next->previous != (Image *) NULL)
-          height=(size_t) MagickMax((ssize_t) height+offset,0U);
+          height=(size_t) MagickMax((ssize_t) height+offset,(ssize_t) 0);
         continue;
       }
     width+=next->columns;
     if (next->previous != (Image *) NULL)
-      width=(size_t) MagickMax((ssize_t) width+offset,0U);
+      width=(size_t) MagickMax((ssize_t) width+offset,(ssize_t) 0);
     if (next->rows > height)
       height=next->rows;
   }
@@ -3856,8 +3863,9 @@ MagickExport Image *SmushImages(const Image *images,
         x_offset-=SmushXGap(smush_image,image,offset,exception);
         y_offset-=geometry.y;
       }
-    status=CompositeImage(smush_image,image,OverCompositeOp,MagickTrue,x_offset,
-      y_offset,exception);
+    if (CompositeImage(smush_image,image,OverCompositeOp,MagickTrue,x_offset,
+          y_offset,exception) == MagickFalse)
+      status=MagickFalse;  /* do not let a later success mask a failure */
     proceed=SetImageProgress(image,SmushImageTag,n,number_images);
     if (proceed == MagickFalse)
       break;
diff --git a/MagickCore/image.h b/MagickCore/image.h
index 8ecf6acfa..234d7da91 100644
--- a/MagickCore/image.h
+++ b/MagickCore/image.h
@@ -225,7 +225,7 @@ struct _Image
     ticks_per_second;  /* units for delay time, default 100 for GIF */

   size_t
-    iterations,        /* number of interactions for GIF animations */
+    iterations,        /* number of iterations for GIF animations */
     total_colors;

   ssize_t
@@ -322,7 +322,7 @@ struct _Image
   struct _ImageInfo
     *image_info;       /* (Optional) Image belongs to this ImageInfo 'list'
                         * For access to 'global options' when no per-image
-                        * attribute, prosperity, or artifact has been set.
+                        * attribute, property, or artifact has been set.
                         */

   struct _Image
@@ -352,7 +352,7 @@ struct _Image
 /*
   ImageInfo structure:
     Stores an image list, as well as all global settings used by all images
-    held, -- unless overridden for that specific image.  See SyncImagesettings()
+    held, -- unless overridden for that specific image.  See SyncImageSettings()
     which maps any global setting that always overrides specific image settings.
 */
 struct _ImageInfo
@@ -365,7 +365,7 @@ struct _ImageInfo

   MagickBooleanType
     temporary,          /* image file to be deleted after read "ephemeral:" */
-    adjoin,             /* save images to separate scene files */
+    adjoin,             /* save all images to a single multi-image file */
     affirm,
     antialias;

diff --git a/MagickCore/list.c b/MagickCore/list.c
index 7ed09642e..a32a9dba8 100644
--- a/MagickCore/list.c
+++ b/MagickCore/list.c
@@ -974,8 +974,17 @@ MagickExport void InsertImageInList(Image **images,Image *insert)
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",insert->filename);
   if ((*images) == (Image *) NULL)
-    return;
+    {
+      /*
+        Inserting into an empty list makes the insert the list (as documented)
+        rather than silently dropping, and leaking, the caller's images.
+      */
+      *images=insert;
+      return;
+    }
   assert((*images)->signature == MagickCoreSignature);
+  if (GetFirstImageInList(*images) == GetFirstImageInList(insert))
+    return;  /* inserting a list into itself would create a cycle */
   split=SplitImageList(*images);
   AppendImageToList(images,insert);
   AppendImageToList(images,split);
@@ -1030,6 +1039,10 @@ MagickExport Image *NewImageList(void)
 */
 MagickExport void PrependImageToList(Image **images,Image *prepend)
 {
+  assert(images != (Image **) NULL);
+  if (prepend == (Image *) NULL)
+    return;
+  assert(prepend->signature == MagickCoreSignature);
   if (*images == (Image *) NULL)
     {
       *images=prepend;
@@ -1237,6 +1250,8 @@ MagickExport void ReplaceImageInList(Image **images,Image *replace)
   if ((*images) == (Image *) NULL)
     return;
   assert((*images)->signature == MagickCoreSignature);
+  if (GetFirstImageInList(replace) == GetFirstImageInList(*images))
+    return;  /* replacement already in this list: relinking would free it */
   /*
     Link next pointer.
   */
@@ -1298,6 +1313,8 @@ MagickExport void ReplaceImageInListReturnLast(Image **images,Image *replace)
   if ((*images) == (Image *) NULL)
     return;
   assert((*images)->signature == MagickCoreSignature);
+  if (GetFirstImageInList(replace) == GetFirstImageInList(*images))
+    return;  /* replacement already in this list: relinking would free it */
   /*
     Link previous pointer.
   */