Commit 67b254b70 for imagemagick.org

commit 67b254b7013dd76f700bb6b0ca6c1e02f831138d
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Thu Dec 3 22:21:14 2026 -0500

    add an explicit guard against a huge thread limit

diff --git a/MagickCore/random-private.h b/MagickCore/random-private.h
index 86b990ba2..b2e061dad 100644
--- a/MagickCore/random-private.h
+++ b/MagickCore/random-private.h
@@ -18,6 +18,11 @@
 #ifndef MAGICKCORE_RANDOM_PRIVATE_H
 #define MAGICKCORE_RANDOM_PRIVATE_H

+#include <string.h>
+#include "MagickCore/exception-private.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/random_.h"
+#include "MagickCore/resource_.h"
 #include "MagickCore/thread-private.h"

 #if defined(__cplusplus) || defined(c_plusplus)
@@ -41,12 +46,29 @@ extern MagickPrivate void
   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.
+  Callers indexing the array should bound-check against
+  GetRandomInfoTLSCount() for the same reason.
 */
 static inline size_t RandomInfoTLSHeaderSlots(void)
 {
   return((sizeof(size_t)+sizeof(RandomInfo *)-1)/sizeof(RandomInfo *));
 }

+static inline size_t GetRandomInfoTLSCount(RandomInfo **random_info)
+{
+  RandomInfo
+    **base;
+
+  size_t
+    number_threads;
+
+  if (random_info == (RandomInfo **) NULL)
+    return(0);
+  base=random_info-RandomInfoTLSHeaderSlots();
+  (void) memcpy(&number_threads,base,sizeof(number_threads));
+  return(number_threads);
+}
+
 static inline RandomInfo **DestroyRandomInfoTLS(RandomInfo **random_info)
 {
   RandomInfo
@@ -77,10 +99,17 @@ static inline RandomInfo **AcquireRandomInfoTLS(void)
     i,
     number_threads;

+  /*
+    Always allocate at least one slot, and guard the header addition against
+    size_t overflow (AcquireQuantumMemory only checks the multiplication).
+  */
   number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
+  if (number_threads == 0)
+    number_threads=1;
   header_slots=RandomInfoTLSHeaderSlots();
-  base=(RandomInfo **) AcquireQuantumMemory(number_threads+header_slots,
-    sizeof(*base));
+  if (number_threads > (((size_t) -1)/sizeof(*base))-header_slots)
+    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+  base=(RandomInfo **) AcquireQuantumMemory(number_threads+header_slots,sizeof(*base));
   if (base == (RandomInfo **) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   (void) memset(base,0,(number_threads+header_slots)*sizeof(*base));