Commit bb0815c53 for imagemagick.org

commit bb0815c53f75210388a2ed78ea74bf30a973184d
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Thu Dec 3 18:11:56 2026 -0500

    https://github.com/ImageMagick/ImageMagick/security/advisories/GHSA-4952-2pjg-h8h8

diff --git a/MagickWand/wand-view.c b/MagickWand/wand-view.c
index c524b5aff..a3e5c0e22 100644
--- a/MagickWand/wand-view.c
+++ b/MagickWand/wand-view.c
@@ -83,6 +83,9 @@ struct _WandView
   PixelWand
     ***pixel_wands;

+  size_t
+    number_threads;
+
   ExceptionInfo
     *exception;

@@ -133,13 +136,19 @@ WandExport WandView *CloneWandView(const WandView *wand_view)
   (void) FormatLocaleString(clone_view->name,MagickPathExtent,"%s-%.17g",
     WandViewId,(double) clone_view->id);
   clone_view->description=ConstantString(wand_view->description);
-  clone_view->image=CloneImage(wand_view->image,0,0,MagickTrue,
-    wand_view->exception);
+  clone_view->wand=wand_view->wand;
   clone_view->view=CloneCacheView(wand_view->view);
+  clone_view->image=(Image *) GetCacheViewImage(clone_view->view);
   clone_view->extent=wand_view->extent;
   clone_view->exception=AcquireExceptionInfo();
   InheritException(clone_view->exception,wand_view->exception);
-  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
+  clone_view->number_threads=wand_view->number_threads;
+  clone_view->pixel_wands=(PixelWand ***) AcquireQuantumMemory(
+    clone_view->number_threads,sizeof(*clone_view->pixel_wands));
+  if (clone_view->pixel_wands == (PixelWand ***) NULL)
+    ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
+      clone_view->name);
+  for (i=0; i < (ssize_t) clone_view->number_threads; i++)
     clone_view->pixel_wands[i]=ClonePixelWands((const PixelWand **)
       wand_view->pixel_wands[i],wand_view->extent.width);
   clone_view->debug=wand_view->debug;
@@ -173,13 +182,13 @@ WandExport WandView *CloneWandView(const WandView *wand_view)
 */

 static PixelWand ***DestroyPixelsTLS(PixelWand ***pixel_wands,
-  const size_t number_wands)
+  const size_t number_threads,const size_t number_wands)
 {
   ssize_t
     i;

   assert(pixel_wands != (PixelWand ***) NULL);
-  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
+  for (i=0; i < (ssize_t) number_threads; i++)
     if (pixel_wands[i] != (PixelWand **) NULL)
       pixel_wands[i]=DestroyPixelWands(pixel_wands[i],number_wands);
   pixel_wands=(PixelWand ***) RelinquishMagickMemory(pixel_wands);
@@ -191,9 +200,10 @@ WandExport WandView *DestroyWandView(WandView *wand_view)
   assert(wand_view != (WandView *) NULL);
   assert(wand_view->signature == MagickWandSignature);
   wand_view->pixel_wands=DestroyPixelsTLS(wand_view->pixel_wands,
-    wand_view->extent.width);
+    wand_view->number_threads,wand_view->extent.width);
   wand_view->view=DestroyCacheView(wand_view->view);
   wand_view->exception=DestroyExceptionInfo(wand_view->exception);
+  wand_view->description=DestroyString(wand_view->description);
   wand_view->signature=(~MagickWandSignature);
   RelinquishWandId(wand_view->id);
   wand_view=(WandView *) RelinquishMagickMemory(wand_view);
@@ -286,11 +296,16 @@ WandExport MagickBooleanType DuplexTransferWandViewIterator(WandView *source,
   status=MagickTrue;
   progress=0;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  height=(size_t) ((ssize_t) source->extent.height-source->extent.y);
+  height=source->extent.height;
   #pragma omp parallel for schedule(static) shared(progress,status) \
-    magick_number_threads(source_image,destination_image,height,1)
+    num_threads((int) MagickMin((size_t) GetMagickNumberThreads( \
+      source_image,destination_image,height,1), \
+      MagickMin(MagickMin( \
+      source->number_threads,duplex->number_threads), \
+      destination->number_threads)))
 #endif
-  for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
+  for (y=source->extent.y; y < (source->extent.y+(ssize_t)
+      source->extent.height); y++)
   {
     const int
       id = GetOpenMPThreadId();
@@ -322,8 +337,9 @@ WandExport MagickBooleanType DuplexTransferWandViewIterator(WandView *source,
       PixelSetQuantumPixel(source->image,pixels,source->pixel_wands[id][x]);
       pixels+=(ptrdiff_t) GetPixelChannels(source->image);
     }
-    duplex_pixels=GetCacheViewVirtualPixels(duplex->view,duplex->extent.x,y,
-      duplex->extent.width,1,duplex->exception);
+    duplex_pixels=GetCacheViewVirtualPixels(duplex->view,duplex->extent.x,
+      duplex->extent.y+(y-source->extent.y),duplex->extent.width,1,
+      duplex->exception);
     if (duplex_pixels == (const Quantum *) NULL)
       {
         status=MagickFalse;
@@ -336,8 +352,8 @@ WandExport MagickBooleanType DuplexTransferWandViewIterator(WandView *source,
       duplex_pixels+=(ptrdiff_t) GetPixelChannels(duplex->image);
     }
     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
-      destination->extent.x,y,destination->extent.width,1,
-      destination->exception);
+      destination->extent.x,destination->extent.y+(y-source->extent.y),
+      destination->extent.width,1,destination->exception);
     if (destination_pixels == (Quantum *) NULL)
       {
         status=MagickFalse;
@@ -352,8 +368,8 @@ WandExport MagickBooleanType DuplexTransferWandViewIterator(WandView *source,
     if (transfer(source,duplex,destination,y,id,context) == MagickFalse)
       status=MagickFalse;
     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
-      destination->extent.x,y,destination->extent.width,1,
-      destination->exception);
+      destination->extent.x,destination->extent.y+(y-source->extent.y),
+      destination->extent.width,1,destination->exception);
     for (x=0; x < (ssize_t) destination->extent.width; x++)
     {
       PixelGetQuantumPixel(destination->image,destination->pixel_wands[id][x],
@@ -540,11 +556,13 @@ WandExport MagickBooleanType GetWandViewIterator(WandView *source,
   status=MagickTrue;
   progress=0;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  height=(size_t) ((ssize_t) source->extent.height-source->extent.y);
+  height=source->extent.height;
   #pragma omp parallel for schedule(static) shared(progress,status) \
-    magick_number_threads(source_image,source_image,height,1)
+    num_threads((int) MagickMin((size_t) GetMagickNumberThreads( \
+      source_image,source_image,height,1),source->number_threads))
 #endif
-  for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
+  for (y=source->extent.y; y < (source->extent.y+(ssize_t)
+      source->extent.height); y++)
   {
     const int
       id = GetOpenMPThreadId();
@@ -712,18 +730,15 @@ WandExport MagickBooleanType IsWandView(const WandView *wand_view)
 %
 */

-static PixelWand ***AcquirePixelsTLS(const size_t number_wands)
+static PixelWand ***AcquirePixelsTLS(const size_t number_threads,
+  const size_t number_wands)
 {
   PixelWand
     ***pixel_wands;

-  size_t
-    number_threads;
-
   ssize_t
     i;

-  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
   pixel_wands=(PixelWand ***) AcquireQuantumMemory(number_threads,
     sizeof(*pixel_wands));
   if (pixel_wands == (PixelWand ***) NULL)
@@ -733,7 +748,7 @@ static PixelWand ***AcquirePixelsTLS(const size_t number_wands)
   {
     pixel_wands[i]=NewPixelWands(number_wands);
     if (pixel_wands[i] == (PixelWand **) NULL)
-      return(DestroyPixelsTLS(pixel_wands,number_wands));
+      return(DestroyPixelsTLS(pixel_wands,number_threads,number_wands));
   }
   return(pixel_wands);
 }
@@ -760,7 +775,9 @@ WandExport WandView *NewWandView(MagickWand *wand)
   wand_view->image=(Image *) GetCacheViewImage(wand_view->view);
   wand_view->extent.width=wand_view->image->columns;
   wand_view->extent.height=wand_view->image->rows;
-  wand_view->pixel_wands=AcquirePixelsTLS(wand_view->extent.width);
+  wand_view->number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
+  wand_view->pixel_wands=AcquirePixelsTLS(wand_view->number_threads,
+    wand_view->extent.width);
   wand_view->exception=exception;
   if (wand_view->pixel_wands == (PixelWand ***) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
@@ -814,15 +831,18 @@ WandExport WandView *NewWandViewExtent(MagickWand *wand,const ssize_t x,
   (void) FormatLocaleString(wand_view->name,MagickPathExtent,"%s-%.17g",
     WandViewId,(double) wand_view->id);
   wand_view->description=ConstantString("WandView");
+  wand_view->wand=wand;
   exception=AcquireExceptionInfo();
   wand_view->view=AcquireVirtualCacheView(wand_view->wand->images,exception);
-  wand_view->wand=wand;
+  wand_view->image=(Image *) GetCacheViewImage(wand_view->view);
   wand_view->extent.width=width;
   wand_view->extent.height=height;
   wand_view->extent.x=x;
   wand_view->extent.y=y;
   wand_view->exception=exception;
-  wand_view->pixel_wands=AcquirePixelsTLS(wand_view->extent.width);
+  wand_view->number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
+  wand_view->pixel_wands=AcquirePixelsTLS(wand_view->number_threads,
+    wand_view->extent.width);
   if (wand_view->pixel_wands == (PixelWand ***) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
       (char *) NULL);
@@ -858,9 +878,14 @@ WandExport WandView *NewWandViewExtent(MagickWand *wand,const ssize_t x,
 MagickExport void SetWandViewDescription(WandView *wand_view,
   const char *description)
 {
+  char
+    *clone_description;
+
   assert(wand_view != (WandView *) NULL);
   assert(wand_view->signature == MagickWandSignature);
-  wand_view->description=ConstantString(description);
+  clone_description=ConstantString(description);
+  wand_view->description=DestroyString(wand_view->description);
+  wand_view->description=clone_description;
 }

 /*
@@ -939,11 +964,14 @@ WandExport MagickBooleanType SetWandViewIterator(WandView *destination,
   status=MagickTrue;
   progress=0;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  height=(size_t) ((ssize_t) destination->extent.height-destination->extent.y);
+  height=destination->extent.height;
   #pragma omp parallel for schedule(static) shared(progress,status) \
-    magick_number_threads(destination_image,destination_image,height,1)
+    num_threads((int) MagickMin((size_t) GetMagickNumberThreads( \
+      destination_image,destination_image,height,1), \
+      destination->number_threads))
 #endif
-  for (y=destination->extent.y; y < (ssize_t) destination->extent.height; y++)
+  for (y=destination->extent.y; y < (destination->extent.y+(ssize_t)
+      destination->extent.height); y++)
   {
     const int
       id = GetOpenMPThreadId();
@@ -1077,11 +1105,15 @@ WandExport MagickBooleanType TransferWandViewIterator(WandView *source,
   status=MagickTrue;
   progress=0;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  height=(size_t) ((ssize_t) source->extent.height-source->extent.y);
+  height=source->extent.height;
   #pragma omp parallel for schedule(static) shared(progress,status) \
-    magick_number_threads(source_image,destination_image,height,1)
+    num_threads((int) MagickMin((size_t) GetMagickNumberThreads( \
+      source_image,destination_image,height,1), \
+      MagickMin( \
+      source->number_threads,destination->number_threads)))
 #endif
-  for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
+  for (y=source->extent.y; y < (source->extent.y+(ssize_t)
+      source->extent.height); y++)
   {
     const int
       id = GetOpenMPThreadId();
@@ -1113,8 +1145,8 @@ WandExport MagickBooleanType TransferWandViewIterator(WandView *source,
       pixels+=(ptrdiff_t) GetPixelChannels(source->image);
     }
     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
-      destination->extent.x,y,destination->extent.width,1,
-      destination->exception);
+      destination->extent.x,destination->extent.y+(y-source->extent.y),
+      destination->extent.width,1,destination->exception);
     if (destination_pixels == (Quantum *) NULL)
       {
         status=MagickFalse;
@@ -1129,8 +1161,8 @@ WandExport MagickBooleanType TransferWandViewIterator(WandView *source,
     if (transfer(source,destination,y,id,context) == MagickFalse)
       status=MagickFalse;
     destination_pixels=GetCacheViewAuthenticPixels(destination->view,
-      destination->extent.x,y,destination->extent.width,1,
-      destination->exception);
+      destination->extent.x,destination->extent.y+(y-source->extent.y),
+      destination->extent.width,1,destination->exception);
     for (x=0; x < (ssize_t) destination->extent.width; x++)
     {
       PixelGetQuantumPixel(destination->image,destination->pixel_wands[id][x],
@@ -1232,11 +1264,13 @@ WandExport MagickBooleanType UpdateWandViewIterator(WandView *source,
   status=MagickTrue;
   progress=0;
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  height=(size_t) ((ssize_t) source->extent.height-source->extent.y);
+  height=source->extent.height;
   #pragma omp parallel for schedule(static) shared(progress,status) \
-    magick_number_threads(source_image,source_image,height,1)
+    num_threads((int) MagickMin((size_t) GetMagickNumberThreads( \
+      source_image,source_image,height,1),source->number_threads))
 #endif
-  for (y=source->extent.y; y < (ssize_t) source->extent.height; y++)
+  for (y=source->extent.y; y < (source->extent.y+(ssize_t)
+      source->extent.height); y++)
   {
     const int
       id = GetOpenMPThreadId();