Commit 2c200afc7 for imagemagick.org

commit 2c200afc72d21a88b25d12d402eda48b6233efa7
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Mon Nov 23 12:43:29 2026 -0500

    https://github.com/ImageMagick/ImageMagick/issues/9142

diff --git a/MagickCore/fx.c b/MagickCore/fx.c
index 01d5415bb..f360a4b0b 100644
--- a/MagickCore/fx.c
+++ b/MagickCore/fx.c
@@ -760,6 +760,7 @@ struct _FxInfo {
   ExceptionInfo * exception;

   fxRtT * fxrts;
+  size_t number_threads;  /* number of fxrts slots, fixed at allocation time. */
 };

 /* Forward declarations for recursion.
@@ -4279,7 +4280,13 @@ MagickPrivate MagickBooleanType FxEvaluateChannelExpression (
   assert (pfx->Images != NULL);
   assert (pfx->Imgs != NULL);
   assert (pfx->fxrts != NULL);
-  assert (id >= 0 && id < (int) GetMagickResourceLimit(ThreadResource));
+
+  if ((id < 0) || ((size_t) id >= pfx->number_threads)) {
+    (void) ThrowMagickException (
+      exception, GetMagickModule(), OptionError,
+      "thread id exceeds FxInfo runtime slots", "%i", id);
+    return MagickFalse;
+  }

   pfx->fxrts[id].thisPixel = NULL;

@@ -4378,6 +4385,9 @@ static FxInfo *AcquireFxInfoPrivate (const Image * images, const char * expressi
   }

   {
+    /* The slot count is recorded in pfx and used for every later access
+       and for destruction, because the ThreadResource limit can change
+       at runtime. */
     size_t number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
     ssize_t t;

@@ -4394,6 +4404,7 @@ static FxInfo *AcquireFxInfoPrivate (const Image * images, const char * expressi
       pfx = (FxInfo*) RelinquishMagickMemory(pfx);
       return NULL;
     }
+    pfx->number_threads = number_threads;
     for (t=0; t < (ssize_t) number_threads; t++) {
       if (!AllocFxRt (pfx, &pfx->fxrts[t])) {
         (void) ThrowMagickException (
@@ -4434,10 +4445,11 @@ FxInfo *DestroyFxInfo (FxInfo * pfx)
   assert (pfx->Imgs != NULL);
   assert (pfx->fxrts != NULL);

-  for (t=0; t < (ssize_t) GetMagickResourceLimit(ThreadResource); t++) {
+  for (t=0; t < (ssize_t) pfx->number_threads; t++) {
     DestroyFxRt (&pfx->fxrts[t]);
   }
   pfx->fxrts = (fxRtT *) RelinquishMagickMemory (pfx->fxrts);
+  pfx->number_threads = 0;

   DestroyRPN (pfx);

@@ -4530,6 +4542,14 @@ MagickExport Image *FxImage(const Image *image,const char *expression,

     if (status == MagickFalse)
       continue;
+    if ((id < 0) || ((size_t) id >= pfx->number_threads)) {
+      /* ThreadResource was raised after pfx was acquired. */
+      (void) ThrowMagickException (
+        pfx->exception, GetMagickModule(), OptionError,
+        "thread id exceeds FxInfo runtime slots", "%i", id);
+      status=MagickFalse;
+      continue;
+    }
     p = GetCacheViewVirtualPixels (image_view, 0, y, image->columns, 1, pfx->exception);
     q = QueueCacheViewAuthenticPixels (fx_view, 0, y, fx_image->columns, 1, pfx->exception);
     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) {
@@ -4593,7 +4613,7 @@ MagickExport Image *FxImage(const Image *image,const char *expression,
     int t, i;
     char UserSym[MagickPathExtent];
     fprintf (stderr, "User symbols (%i):\n", pfx->usedUserSymbols);
-    for (t=0; t < (int) GetMagickResourceLimit(ThreadResource); t++) {
+    for (t=0; t < (int) pfx->number_threads; t++) {
       for (i = 0; i < (int) pfx->usedUserSymbols; i++) {
         fprintf (stderr, "th=%i us=%i '%s': %.*Lg\n",
                  t, i, NameOfUserSym (pfx, i, UserSym), pfx->precision, pfx->fxrts[t].UserSymVals[i]);
diff --git a/MagickCore/random-private.h b/MagickCore/random-private.h
index 5d7fbca1b..86b990ba2 100644
--- a/MagickCore/random-private.h
+++ b/MagickCore/random-private.h
@@ -36,35 +36,57 @@ extern MagickPrivate unsigned long
 extern MagickPrivate void
   RandomComponentTerminus(void);

+/*
+  The per-thread RandomInfo array carries its own element count in a hidden
+  header that precedes the first slot.  Destruction must use this recorded
+  count rather than re-reading the (process-wide, runtime-writable)
+  ThreadResource limit, which may have changed since the array was acquired.
+*/
+static inline size_t RandomInfoTLSHeaderSlots(void)
+{
+  return((sizeof(size_t)+sizeof(RandomInfo *)-1)/sizeof(RandomInfo *));
+}
+
 static inline RandomInfo **DestroyRandomInfoTLS(RandomInfo **random_info)
 {
-  ssize_t
-    i;
+  RandomInfo
+    **base;

-  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
+  size_t
+    i,
+    number_threads;
+
+  if (random_info == (RandomInfo **) NULL)
+    return((RandomInfo **) NULL);
+  base=random_info-RandomInfoTLSHeaderSlots();
+  (void) memcpy(&number_threads,base,sizeof(number_threads));
+  for (i=0; i < number_threads; i++)
     if (random_info[i] != (RandomInfo *) NULL)
       random_info[i]=DestroyRandomInfo(random_info[i]);
-  return((RandomInfo **) RelinquishMagickMemory(random_info));
+  return((RandomInfo **) RelinquishMagickMemory(base));
 }

 static inline RandomInfo **AcquireRandomInfoTLS(void)
 {
-  ssize_t
-    i;
-
   RandomInfo
+    **base,
     **random_info;

   size_t
+    header_slots,
+    i,
     number_threads;

   number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
-  random_info=(RandomInfo **) AcquireQuantumMemory(number_threads,
-    sizeof(*random_info));
-  if (random_info == (RandomInfo **) NULL)
+  header_slots=RandomInfoTLSHeaderSlots();
+  base=(RandomInfo **) AcquireQuantumMemory(number_threads+header_slots,
+    sizeof(*base));
+  if (base == (RandomInfo **) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
-  (void) memset(random_info,0,number_threads*sizeof(*random_info));
-  for (i=0; i < (ssize_t) number_threads; i++)
+  (void) memset(base,0,(number_threads+header_slots)*sizeof(*base));
+  (void) memcpy(base,&number_threads,sizeof(number_threads));
+  random_info=base+header_slots;
+  for (i=0; i < number_threads; i++)
     random_info[i]=AcquireRandomInfo();
   return(random_info);
 }