Commit a6b9e7f72 for imagemagick.org

commit a6b9e7f72e00fddb59a3ca8934e78cf8e9bcf59c
Author: Dirk Lemstra <dirk@lemstra.org>
Date:   Fri Aug 21 22:24:09 2026 +0200

    Report errors or return null instead of exiting the process.

diff --git a/MagickCore/cache.c b/MagickCore/cache.c
index 8d1b4c184..e1bad764d 100644
--- a/MagickCore/cache.c
+++ b/MagickCore/cache.c
@@ -659,7 +659,7 @@ static MagickBooleanType ClonePixelCacheOnDisk(
     }
   buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
   if (buffer == (unsigned char *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    return(MagickFalse);
   extent=0;
   while (extent < cache_info->length)
   {
diff --git a/MagickCore/compare.c b/MagickCore/compare.c
index c3d1ff19e..9ad2847a1 100644
--- a/MagickCore/compare.c
+++ b/MagickCore/compare.c
@@ -4597,7 +4597,7 @@ static double GetSimilarityMetric(const Image *image,
   channel_similarity=(double *) AcquireQuantumMemory(length,
     sizeof(*channel_similarity));
   if (channel_similarity == (double *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    return(NAN);
   (void) memset(channel_similarity,0,length*sizeof(*channel_similarity));
   switch (metric)
   {
diff --git a/MagickCore/feature.c b/MagickCore/feature.c
index d4529eb14..5d80f3c81 100644
--- a/MagickCore/feature.c
+++ b/MagickCore/feature.c
@@ -672,7 +672,11 @@ MagickExport ChannelFeatures *GetImageFeatures(const Image *image,
   channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
     sizeof(*channel_features));
   if (channel_features == (ChannelFeatures *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),
+        ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+      return(channel_features);
+    }
   (void) memset(channel_features,0,length*
     sizeof(*channel_features));
   /*
diff --git a/MagickCore/identify.c b/MagickCore/identify.c
index cdcb7f98a..a103eade1 100644
--- a/MagickCore/identify.c
+++ b/MagickCore/identify.c
@@ -149,7 +149,11 @@ static ChannelStatistics *GetLocationStatistics(const Image *image,
   channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
     MaxPixelChannels+1,sizeof(*channel_statistics));
   if (channel_statistics == (ChannelStatistics *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),
+        ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
+      return(channel_statistics);
+    }
   (void) memset(channel_statistics,0,(MaxPixelChannels+1)*
     sizeof(*channel_statistics));
   for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
diff --git a/MagickCore/opencl.c b/MagickCore/opencl.c
index 80a6431ae..d7ab1ad84 100644
--- a/MagickCore/opencl.c
+++ b/MagickCore/opencl.c
@@ -1690,7 +1690,10 @@ static MagickBooleanType RegisterCacheEvent(MagickCLCacheInfo info,
     info->events=(cl_event *) ResizeQuantumMemory(info->events,
       ++info->event_count,sizeof(*info->events));
   if (info->events == (cl_event *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    {
+      UnlockSemaphoreInfo(info->events_semaphore);
+      return(MagickFalse);
+    }
   info->events[info->event_count-1]=event;
   UnlockSemaphoreInfo(info->events_semaphore);
   return(MagickTrue);
@@ -2785,7 +2788,13 @@ MagickPrivate MagickBooleanType RecordProfileData(MagickCLDevice device,
       device->profile_records=(KernelProfileRecord *) ResizeQuantumMemory(
         device->profile_records,(i+2),sizeof(*device->profile_records));
       if (device->profile_records == (KernelProfileRecord *) NULL)
-        ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+        {
+          UnlockSemaphoreInfo(device->lock);
+          profile_record=(KernelProfileRecord) RelinquishMagickMemory(
+            profile_record);
+          name=DestroyString(name);
+          return(MagickFalse);
+        }
       device->profile_records[i]=profile_record;
       device->profile_records[i+1]=(KernelProfileRecord) NULL;
     }
diff --git a/MagickCore/string.c b/MagickCore/string.c
index 5147d62e0..07ca6fe8f 100644
--- a/MagickCore/string.c
+++ b/MagickCore/string.c
@@ -2385,7 +2385,7 @@ MagickExport char **StringToStrings(const char *text,size_t *count)
       textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
         sizeof(*textlist));
       if (textlist == (char **) NULL)
-        ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
+        return((char **) NULL);
       p=text;
       for (i=0; i < (ssize_t) lines; i++)
       {
@@ -2395,7 +2395,15 @@ MagickExport char **StringToStrings(const char *text,size_t *count)
         textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+1,
           sizeof(**textlist));
         if (textlist[i] == (char *) NULL)
-          ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
+          {
+            ssize_t
+              j;
+
+            for (j=0; j < i; j++)
+              textlist[j]=DestroyString(textlist[j]);
+            textlist=(char **) RelinquishMagickMemory(textlist);
+            return((char **) NULL);
+          }
         (void) memcpy(textlist[i],p,(size_t) (q-p));
         textlist[i][q-p]='\0';
         if (*q == '\r')
@@ -2421,7 +2429,7 @@ MagickExport char **StringToStrings(const char *text,size_t *count)
       textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
         sizeof(*textlist));
       if (textlist == (char **) NULL)
-        ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
+        return((char **) NULL);
       p=text;
       for (i=0; i < (ssize_t) lines; i++)
       {
@@ -2431,7 +2439,12 @@ MagickExport char **StringToStrings(const char *text,size_t *count)
         textlist[i]=(char *) AcquireQuantumMemory(2UL*MagickPathExtent,
           sizeof(**textlist));
         if (textlist[i] == (char *) NULL)
-          ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
+          {
+            for (j=0; j < i; j++)
+              textlist[j]=DestroyString(textlist[j]);
+            textlist=(char **) RelinquishMagickMemory(textlist);
+            return((char **) NULL);
+          }
         (void) FormatLocaleString(textlist[i],MagickPathExtent,"0x%08lx: ",
           (long) (CharsPerLine*i));
         q=textlist[i]+strlen(textlist[i]);
@@ -2461,10 +2474,22 @@ MagickExport char **StringToStrings(const char *text,size_t *count)
           p++;
         }
         *q='\0';
-        textlist[i]=(char *) ResizeQuantumMemory(textlist[i],(size_t) (q-
-          textlist[i]+1),sizeof(**textlist));
-        if (textlist[i] == (char *) NULL)
-          ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
+        {
+          char
+            *resized;
+
+          resized=(char *) ResizeQuantumMemory(textlist[i],(size_t) (q-
+            textlist[i]+1),sizeof(**textlist));
+          if (resized == (char *) NULL)
+            {
+              textlist[i]=DestroyString(textlist[i]);
+              for (j=0; j < i; j++)
+                textlist[j]=DestroyString(textlist[j]);
+              textlist=(char **) RelinquishMagickMemory(textlist);
+              return((char **) NULL);
+            }
+          textlist[i]=resized;
+        }
       }
     }
   if (count != (size_t *) NULL)
diff --git a/MagickCore/utility.c b/MagickCore/utility.c
index 7a84c0c74..4d0e1967f 100644
--- a/MagickCore/utility.c
+++ b/MagickCore/utility.c
@@ -1501,7 +1501,7 @@ MagickPrivate char **GetPathComponents(const char *path,
   components=(char **) AcquireQuantumMemory((size_t) *number_components+1UL,
     sizeof(*components));
   if (components == (char **) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    return((char **) NULL);
   p=path;
   for (i=0; i < (ssize_t) *number_components; i++)
   {
@@ -1510,8 +1510,16 @@ MagickPrivate char **GetPathComponents(const char *path,
         break;
     components[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
       sizeof(**components));
-    if (components[i] == (char *) NULL)
-      ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    if (components[i] == (char*)NULL)
+      {
+        ssize_t
+          j;
+
+        for (j=0; j < i; j++)
+          components[j]=DestroyString(components[j]);
+        components=(char **) RelinquishMagickMemory(components);
+        return((char **) NULL);
+      }
     (void) CopyMagickString(components[i],p,(size_t) (q-p+1));
     p=q+1;
   }
@@ -1811,7 +1819,11 @@ MagickPrivate char **ListFiles(const char *directory,const char *pattern,
   */
   buffer=(struct dirent *) AcquireMagickMemory(sizeof(*buffer)+FILENAME_MAX+1);
   if (buffer == (struct dirent *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    {
+      filelist=(char **) RelinquishMagickMemory(filelist);
+      (void) closedir(current_directory);
+      return((char **) NULL);
+    }
   while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
          (entry != (struct dirent *) NULL))
   {
diff --git a/coders/jpeg.c b/coders/jpeg.c
index abefdea74..e1c04ce36 100644
--- a/coders/jpeg.c
+++ b/coders/jpeg.c
@@ -2595,8 +2595,12 @@ static QuantizationTable *GetQuantizationTable(const char *filename,
   table->levels=(unsigned int *) AcquireQuantumMemory(length,
     sizeof(*table->levels));
   if (table->levels == (unsigned int *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,
-      "UnableToAcquireQuantizationTable");
+    {
+      quantization_tables=DestroyXMLTree(quantization_tables);
+      table=DestroyQuantizationTable(table);
+      xml=DestroyString(xml);
+      return(table);
+    }
   for (i=0; i < (ssize_t) (table->width*table->height); i++)
   {
     table->levels[i]=(unsigned int) (InterpretLocaleValue(content,&p)/
@@ -2826,7 +2830,7 @@ static char **SamplingFactorToList(const char *text)
   textlist=(char **) AcquireQuantumMemory((size_t) MAX_COMPONENTS,
     sizeof(*textlist));
   if (textlist == (char **) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
+    return((char **) NULL);
   p=text;
   for (i=0; i < (ssize_t) MAX_COMPONENTS; i++)
   {
@@ -2836,7 +2840,15 @@ static char **SamplingFactorToList(const char *text)
     textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MagickPathExtent,
       sizeof(*textlist[i]));
     if (textlist[i] == (char *) NULL)
-      ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
+      {
+        ssize_t
+          j;
+
+        for (j=0; j < i; j++)
+          textlist[j]=DestroyString(textlist[j]);
+        textlist=(char **) RelinquishMagickMemory(textlist);
+        return(textlist);
+      }
     (void) CopyMagickString(textlist[i],p,(size_t) (q-p+1));
     if (*q == '\r')
       q++;
diff --git a/coders/json.c b/coders/json.c
index cc4c87739..cca034c50 100644
--- a/coders/json.c
+++ b/coders/json.c
@@ -334,7 +334,7 @@ static ChannelStatistics *GetLocationStatistics(const Image *image,
   channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
     MaxPixelChannels+1,sizeof(*channel_statistics));
   if (channel_statistics == (ChannelStatistics *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    return((ChannelStatistics *) NULL);
   (void) memset(channel_statistics,0,(MaxPixelChannels+1)*
     sizeof(*channel_statistics));
   for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
diff --git a/coders/yaml.c b/coders/yaml.c
index 65c0aeb0f..2f1350cc5 100644
--- a/coders/yaml.c
+++ b/coders/yaml.c
@@ -335,7 +335,7 @@ static ChannelStatistics *GetLocationStatistics(const Image *image,
   channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
     MaxPixelChannels+1,sizeof(*channel_statistics));
   if (channel_statistics == (ChannelStatistics *) NULL)
-    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
+    return((ChannelStatistics *) NULL);
   (void) memset(channel_statistics,0,(MaxPixelChannels+1)*
     sizeof(*channel_statistics));
   for (i=0; i <= (ssize_t) MaxPixelChannels; i++)