Commit a717fb88b for imagemagick.org
commit a717fb88b984529d5a3f25c9cc78e57edf587ef8
Author: Cristy <urban-warrior@imagemagick.org>
Date: Thu Dec 3 12:37:51 2026 -0500
https://github.com/ImageMagick/ImageMagick/issues/9143
diff --git a/MagickCore/magick.c b/MagickCore/magick.c
index 3cb58c025..db7d25cee 100644
--- a/MagickCore/magick.c
+++ b/MagickCore/magick.c
@@ -126,9 +126,11 @@ static SplayTreeInfo
static volatile MagickBooleanType
magickcore_instantiated = MagickFalse,
- magickcore_signal_in_progress = MagickFalse,
magick_list_initialized = MagickFalse;
+static volatile sig_atomic_t
+ magickcore_signal_in_progress = 0;
+
static int
magick_precision = 0;
@@ -348,8 +350,7 @@ MagickExport MagickBooleanType GetMagickAdjoin(const MagickInfo *magick_info)
if (magick_info == (const MagickInfo *) NULL)
return(MagickFalse);
assert(magick_info->signature == MagickCoreSignature);
- return(((magick_info->flags & CoderAdjoinFlag) == 0) ? MagickFalse :
- MagickTrue);
+ return(((magick_info->flags & CoderAdjoinFlag) == 0) ? MagickFalse : MagickTrue);
}
/*
@@ -693,7 +694,7 @@ static int MagickInfoCompare(const void *x,const void *y)
**p,
**q;
- p=(const MagickInfo **) x,
+ p=(const MagickInfo **) x;
q=(const MagickInfo **) y;
return(LocaleCompare((*p)->name,(*q)->name));
}
@@ -748,7 +749,7 @@ MagickExport const MagickInfo **GetMagickInfoList(const char *pattern,
}
UnlockSemaphoreInfo(magick_semaphore);
qsort((void *) formats,(size_t) i,sizeof(*formats),MagickInfoCompare);
- formats[i]=(MagickInfo *) NULL;
+ formats[i]=(const MagickInfo *) NULL;
*number_formats=(size_t) i;
return(formats);
}
@@ -1405,7 +1406,11 @@ static SignalHandler *SetMagickSignalHandler(int signal_number,
sigemptyset(&mask);
sigaddset(&mask,signal_number);
sigemptyset(&previous_mask);
- sigprocmask(SIG_BLOCK,&mask,&previous_mask);
+#if defined(MAGICKCORE_THREAD_SUPPORT)
+ (void) pthread_sigmask(SIG_BLOCK,&mask,&previous_mask);
+#else
+ (void) sigprocmask(SIG_BLOCK,&mask,&previous_mask);
+#endif
action.sa_mask=mask;
action.sa_handler=handler;
action.sa_flags=0;
@@ -1421,7 +1426,11 @@ static SignalHandler *SetMagickSignalHandler(int signal_number,
Always restore the caller's signal mask, including on failure, so the
signal is never left blocked.
*/
- sigprocmask(SIG_SETMASK,&previous_mask,NULL);
+#if defined(MAGICKCORE_THREAD_SUPPORT)
+ (void) pthread_sigmask(SIG_SETMASK,&previous_mask,(sigset_t *) NULL);
+#else
+ (void) sigprocmask(SIG_SETMASK,&previous_mask,(sigset_t *) NULL);
+#endif
if (status < 0)
return(SIG_ERR);
return(previous_action.sa_handler);
@@ -1430,62 +1439,66 @@ static SignalHandler *SetMagickSignalHandler(int signal_number,
#endif
}
-static void MagickSignalHandler(int signal_number)
+static void ResetAndRaiseSignal(int signal_number)
{
- if ((magickcore_signal_in_progress != MagickFalse) &&
- (signal_number >= 0) && (signal_number < SIGMAX))
- (void) SetMagickSignalHandler(signal_number,signal_handlers[signal_number]);
- magickcore_signal_in_progress=MagickTrue;
- AsynchronousResourceComponentTerminus();
-#if defined(SIGQUIT)
- if (signal_number == SIGQUIT)
- abort();
-#endif
-#if defined(SIGABRT)
- if (signal_number == SIGABRT)
- abort();
-#endif
-#if defined(SIGBUS)
- if (signal_number == SIGBUS)
- abort();
-#endif
-#if defined(SIGFPE)
- if (signal_number == SIGFPE)
- abort();
-#endif
-#if defined(SIGSEGV)
- if (signal_number == SIGSEGV)
- abort();
-#endif
-#if !defined(MAGICKCORE_HAVE__EXIT)
- exit(signal_number);
+#if defined(MAGICKCORE_HAVE_SIGACTION) && defined(MAGICKCORE_HAVE_SIGEMPTYSET)
+ sigset_t
+ mask;
+
+ struct sigaction
+ action;
+
+ (void) memset(&action,0,sizeof(action));
+ sigemptyset(&action.sa_mask);
+ action.sa_handler=SIG_DFL;
+ (void) sigaction(signal_number,&action,(struct sigaction *) NULL);
+ sigemptyset(&mask);
+ sigaddset(&mask,signal_number);
+#if defined(MAGICKCORE_THREAD_SUPPORT)
+ (void) pthread_sigmask(SIG_UNBLOCK,&mask,(sigset_t *) NULL);
#else
-#if defined(SIGHUP)
- if (signal_number == SIGHUP)
- _exit(signal_number);
+ (void) sigprocmask(SIG_UNBLOCK,&mask,(sigset_t *) NULL);
#endif
-#if defined(SIGINT)
- if (signal_number == SIGINT)
- {
- signal(SIGINT,SIG_DFL);
- raise(SIGINT);
- _exit(signal_number);
- }
+#else
+ (void) signal(signal_number,SIG_DFL);
#endif
#if defined(MAGICKCORE_HAVE_RAISE)
- if ((signal_number >= 0) && (signal_number < SIGMAX) &&
- (signal_handlers[signal_number] != MagickSignalHandler))
- raise(signal_number);
+ (void) raise(signal_number);
#endif
+}
+
+static void MagickSignalExit(int signal_number)
+{
+#if defined(MAGICKCORE_HAVE__EXIT) || defined(MAGICKCORE_WINDOWS_SUPPORT)
_exit(signal_number); /* do not invoke registered atexit() methods */
+#else
+ exit(signal_number);
#endif
}
+static void MagickSignalHandler(int signal_number)
+{
+ if (magickcore_signal_in_progress != 0)
+ {
+ /*
+ A fault occurred while cleaning up; terminate without re-entering.
+ */
+ ResetAndRaiseSignal(signal_number);
+ MagickSignalExit(signal_number);
+ }
+ magickcore_signal_in_progress=1;
+ AsynchronousResourceComponentTerminus();
+ ResetAndRaiseSignal(signal_number);
+ MagickSignalExit(signal_number);
+}
+
static SignalHandler *RegisterMagickSignalHandler(int signal_number)
{
SignalHandler
*handler;
+ if ((signal_number <= 0) || (signal_number >= SIGMAX))
+ return(SIG_ERR);
handler=SetMagickSignalHandler(signal_number,MagickSignalHandler);
if (handler == SIG_ERR)
return(handler);
diff --git a/MagickCore/resource.c b/MagickCore/resource.c
index eb1466090..3dc6ef9ed 100644
--- a/MagickCore/resource.c
+++ b/MagickCore/resource.c
@@ -70,8 +70,8 @@
/*
Define declarations.
*/
-#define NumberOfResourceTypes \
- (sizeof(resource_semaphore)/sizeof(*resource_semaphore))
+#define NumberOfResourceTypes (sizeof(resource_semaphore)/sizeof(*resource_semaphore))
+#define MaxAsyncTemporaryResources 1024
/*
Typedef declarations.
@@ -156,6 +156,12 @@ static SemaphoreInfo
static SplayTreeInfo
*temporary_resources = (SplayTreeInfo *) NULL;
+
+static char
+ async_temporary_paths[MaxAsyncTemporaryResources][MagickPathExtent];
+
+static volatile sig_atomic_t
+ async_temporary_active[MaxAsyncTemporaryResources];
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -403,7 +409,7 @@ MagickExport MagickBooleanType AcquireMagickResource(const ResourceType type,
%
% AsynchronousResourceComponentTerminus() destroys the resource environment.
% It differs from ResourceComponentTerminus() in that it can be called from a
-% asynchronous signal handler.
+% asynchronous signal handler without acquiring locks or allocating memory.
%
% The format of the AsynchronousResourceComponentTerminus() method is:
%
@@ -412,26 +418,24 @@ MagickExport MagickBooleanType AcquireMagickResource(const ResourceType type,
*/
MagickExport void AsynchronousResourceComponentTerminus(void)
{
- const char
- *path;
+ size_t
+ i;
- if (temporary_resources == (SplayTreeInfo *) NULL)
- return;
/*
Remove any lingering temporary files.
*/
- if (resource_semaphore[FileResource] == (SemaphoreInfo *) NULL)
- ActivateSemaphoreInfo(&resource_semaphore[FileResource]);
- LockSemaphoreInfo(resource_semaphore[FileResource]);
- ResetSplayTreeIterator(temporary_resources);
- path=(const char *) GetNextKeyInSplayTree(temporary_resources);
- while (path != (const char *) NULL)
+ for (i=0; i < MaxAsyncTemporaryResources; i++)
{
- (void) ShredFile(path);
- (void) remove_utf8(path);
- path=(const char *) GetNextKeyInSplayTree(temporary_resources);
+ if (async_temporary_active[i] != 0)
+ {
+ async_temporary_active[i]=0;
+ if (async_temporary_paths[i][0] != '\0')
+ {
+ (void) remove_utf8(async_temporary_paths[i]);
+ async_temporary_paths[i][0]='\0';
+ }
+ }
}
- UnlockSemaphoreInfo(resource_semaphore[FileResource]);
}
/*
@@ -459,14 +463,79 @@ MagickExport void AsynchronousResourceComponentTerminus(void)
%
*/
+static void UnregisterAsyncTemporaryResource(const char *path)
+{
+ size_t
+ i;
+
+ for (i=0; i < MaxAsyncTemporaryResources; i++)
+ {
+ if ((async_temporary_active[i] != 0) && (strcmp(async_temporary_paths[i],path) == 0))
+ {
+ async_temporary_active[i]=0;
+ *async_temporary_paths[i]='\0';
+ return;
+ }
+ }
+}
+
static void *DestroyTemporaryResources(void *temporary_resource)
{
+ UnregisterAsyncTemporaryResource((const char *) temporary_resource);
(void) ShredFile((char *) temporary_resource);
(void) remove_utf8((char *) temporary_resource);
temporary_resource=DestroyString((char *) temporary_resource);
return((void *) NULL);
}
+static void CopyAsyncTemporaryPath(char *destination,const char *source,const size_t length)
+{
+ volatile char
+ *q;
+
+ size_t
+ i;
+
+ /*
+ Volatile stores may not be reordered past the later volatile store.
+ */
+ q=(volatile char *) destination;
+ for (i=0; i < length; i++)
+ q[i]=source[i];
+ q[length]='\0';
+}
+
+static void RegisterAsyncTemporaryResource(const char *path)
+{
+ size_t
+ i,
+ length,
+ slot;
+
+ /*
+ Caller must hold the FileResource semaphore.
+ */
+ length=strlen(path);
+ if (length >= MagickPathExtent)
+ return;
+ slot=MaxAsyncTemporaryResources;
+ for (i=0; i < MaxAsyncTemporaryResources; i++)
+ {
+ if (async_temporary_active[i] == 0)
+ {
+ if (slot == MaxAsyncTemporaryResources)
+ slot=i;
+ continue;
+ }
+ if (strcmp(async_temporary_paths[i],path) == 0)
+ return; /* already registered */
+ }
+ if (slot == MaxAsyncTemporaryResources)
+ return; /* table full; synchronous cleanup still applies */
+ CopyAsyncTemporaryPath(async_temporary_paths[slot],path,length);
+ async_temporary_active[slot]=1;
+}
+
MagickExport int AcquireUniqueFileResource(char *path)
{
int
@@ -563,14 +632,43 @@ MagickExport int AcquireUniqueFileResource(char *path)
if (temporary_resources == (SplayTreeInfo *) NULL)
temporary_resources=NewSplayTree(CompareSplayTreeString,
DestroyTemporaryResources,RelinquishMagickMemory);
- UnlockSemaphoreInfo(resource_semaphore[FileResource]);
attributes=(struct stat *) AcquireCriticalMemory(sizeof(struct stat));
status=GetPathAttributes(path,attributes);
if (status == MagickFalse)
attributes=(struct stat *) RelinquishMagickMemory(attributes);
else
- status=AddValueToSplayTree(temporary_resources,ConstantString(path),
- attributes);
+ {
+ struct stat
+ *stale_attributes;
+
+ /*
+ A stale entry for this path (file removed outside of RelinquishUniqueFileResource)
+ must not be replaced through AddValueToSplayTree().
+ */
+ stale_attributes=(struct stat *) GetValueFromSplayTree(temporary_resources,
+ (const void *) path);
+ if (stale_attributes != (struct stat *) NULL)
+ {
+ *stale_attributes=(*attributes);
+ attributes=(struct stat *) RelinquishMagickMemory(attributes);
+ }
+ else
+ {
+ char
+ *key;
+
+ key=ConstantString(path);
+ status=AddValueToSplayTree(temporary_resources,key,attributes);
+ if (status == MagickFalse)
+ {
+ key=DestroyString(key);
+ attributes=(struct stat *) RelinquishMagickMemory(attributes);
+ }
+ }
+ if (status != MagickFalse)
+ RegisterAsyncTemporaryResource(path);
+ }
+ UnlockSemaphoreInfo(resource_semaphore[FileResource]);
if (status == MagickFalse)
{
(void) close_utf8(file);
@@ -733,13 +831,15 @@ MagickExport MagickBooleanType IsFileResourceIdentityValid(const char *path)
struct stat
attributes;
- if (temporary_resources == (SplayTreeInfo *) NULL)
- return(MagickTrue);
+ if (path == (const char *) NULL)
+ return(MagickFalse);
if (resource_semaphore[FileResource] == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&resource_semaphore[FileResource]);
LockSemaphoreInfo(resource_semaphore[FileResource]);
- temporary_attributes=(const struct stat *) GetValueFromSplayTree(
- temporary_resources,(const void *) path);
+ temporary_attributes=(const struct stat *) NULL;
+ if (temporary_resources != (SplayTreeInfo *) NULL)
+ temporary_attributes=(const struct stat *) GetValueFromSplayTree(
+ temporary_resources,(const void *) path);
if (temporary_attributes != (const struct stat *) NULL)
{
status=GetPathAttributes(path,&attributes);
@@ -862,66 +962,38 @@ MagickExport MagickSizeType GetMagickResourceLimit(const ResourceType type)
%
*/
-static void FormatTimeToLive(const MagickSizeType ttl,char *timeString)
+static void FormatTimeToLive(const MagickSizeType ttl,const size_t extent,
+ char *timeString)
{
- MagickSizeType
- days,
- hours,
- minutes,
- months,
- seconds,
- weeks,
- years;
+ static const struct
+ {
+ MagickSizeType
+ seconds;
- years=ttl/31536000;
- seconds=ttl % 31536000;
- if (seconds == 0)
- {
- (void) FormatLocaleString(timeString,MagickPathExtent,"%llu years",
- (unsigned long long) years);
- return;
- }
- months=ttl/2628000;
- seconds=ttl % 2628000;
- if (seconds == 0)
- {
- (void) FormatLocaleString(timeString,MagickPathExtent,"%llu months",
- (unsigned long long) months);
- return;
- }
- weeks=ttl/604800;
- seconds=ttl % 604800;
- if (seconds == 0)
- {
- (void) FormatLocaleString(timeString,MagickPathExtent,"%llu weeks",
- (unsigned long long) weeks);
- return;
- }
- days=ttl/86400;
- seconds=ttl % 86400;
- if (seconds == 0)
- {
- (void) FormatLocaleString(timeString,MagickPathExtent,"%llu days",
- (unsigned long long) days);
- return;
- }
- hours=ttl/3600;
- seconds=ttl % 3600;
- if (seconds == 0)
- {
- (void) FormatLocaleString(timeString,MagickPathExtent,"%llu hours",
- (unsigned long long) hours);
- return;
- }
- minutes=ttl/60;
- seconds=ttl % 60;
- if (seconds == 0)
- {
- (void) FormatLocaleString(timeString,MagickPathExtent,"%llu minutes",
- (unsigned long long) minutes);
- return;
- }
- (void) FormatLocaleString(timeString,MagickPathExtent,"%llu seconds",
+ const char
+ *units;
+ } periods[] =
+ {
+ { 31536000, "years" },
+ { 2628000, "months" },
+ { 604800, "weeks" },
+ { 86400, "days" },
+ { 3600, "hours" },
+ { 60, "minutes" }
+ };
+
+ size_t
+ i;
+
+ if (ttl != 0)
+ for (i=0; i < (sizeof(periods)/sizeof(*periods)); i++)
+ if ((ttl % periods[i].seconds) == 0)
+ {
+ (void) FormatLocaleString(timeString,extent,"%llu %s",
+ (unsigned long long) (ttl/periods[i].seconds),periods[i].units);
+ return;
+ }
+ (void) FormatLocaleString(timeString,extent,"%llu seconds",
(unsigned long long) ttl);
}
@@ -965,7 +1037,7 @@ MagickExport MagickBooleanType ListMagickResourceInfo(FILE *file,
MagickFormatExtent,disk_limit);
(void) CopyMagickString(time_limit,"unlimited",MagickFormatExtent);
if (resource_info.time_limit != MagickResourceInfinity)
- FormatTimeToLive(resource_info.time_limit,time_limit);
+ FormatTimeToLive(resource_info.time_limit,MagickFormatExtent,time_limit);
(void) FormatLocaleFile(file,"Resource limits:\n");
(void) FormatLocaleFile(file," Width: %s\n",width_limit);
(void) FormatLocaleFile(file," Height: %s\n",height_limit);
@@ -1163,6 +1235,7 @@ MagickExport MagickBooleanType RelinquishUniqueFileResource(const char *path)
if (resource_semaphore[FileResource] == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&resource_semaphore[FileResource]);
LockSemaphoreInfo(resource_semaphore[FileResource]);
+ UnregisterAsyncTemporaryResource(path);
if (temporary_resources != (SplayTreeInfo *) NULL)
status=DeleteNodeFromSplayTree(temporary_resources,(const void *) path);
UnlockSemaphoreInfo(resource_semaphore[FileResource]);
@@ -1221,6 +1294,11 @@ MagickPrivate MagickBooleanType ResourceComponentGenesis(void)
for (i=0; i < (ssize_t) NumberOfResourceTypes; i++)
if (resource_semaphore[i] == (SemaphoreInfo *) NULL)
resource_semaphore[i]=AcquireSemaphoreInfo();
+ for (i=0; i < (ssize_t) MaxAsyncTemporaryResources; i++)
+ {
+ async_temporary_active[i]=0;
+ async_temporary_paths[i][0]='\0';
+ }
(void) SetMagickResourceLimit(WidthResource,resource_info.width_limit);
limit=GetEnvironmentValue("MAGICK_WIDTH_LIMIT");
if (limit != (char *) NULL)
@@ -1416,6 +1494,11 @@ MagickPrivate void ResourceComponentTerminus(void)
LockSemaphoreInfo(resource_semaphore[FileResource]);
if (temporary_resources != (SplayTreeInfo *) NULL)
temporary_resources=DestroySplayTree(temporary_resources);
+ for (i=0; i < (ssize_t) MaxAsyncTemporaryResources; i++)
+ {
+ async_temporary_active[i]=0;
+ async_temporary_paths[i][0]='\0';
+ }
if (random_info != (RandomInfo *) NULL)
random_info=DestroyRandomInfo(random_info);
UnlockSemaphoreInfo(resource_semaphore[FileResource]);