Commit e6a102113 for imagemagick.org

commit e6a1021131e45e674d0a4d18ca7cb91a85cdc1b6
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat Dec 5 13:02:04 2026 -0500

    delegates with no command are now rejected + The -dispose option was being ignored

diff --git a/MagickCore/constitute-private.h b/MagickCore/constitute-private.h
index 2a4d57ecd..2f3b118b7 100644
--- a/MagickCore/constitute-private.h
+++ b/MagickCore/constitute-private.h
@@ -59,17 +59,27 @@ static inline MagickBooleanType CheckCoderDepth(ImageInfo *image_info,
   return(MagickTrue);
 }

-static inline Image *StrictReadImage(ImageInfo *image_info,
+/*
+  Release one level of coder nesting acquired with CheckCoderDepth().  The
+  Strict*() wrappers below call this once the nested read returns, so the
+  counter measures nesting depth rather than the total number of reads made
+  with the same image_info (a coder that reads many sub-images in a loop must
+  not trip the limit).
+*/
+static inline void ReleaseCoderDepth(ImageInfo *image_info)
+{
+  assert(image_info != (ImageInfo *) NULL);
+  assert(image_info->signature == MagickCoreSignature);
+  if (image_info->coder_depth > 0)
+    image_info->coder_depth--;
+}
+
+static inline Image *StrictReadImageInternal(ImageInfo *image_info,
   ExceptionInfo *exception)
 {
   char
     magic[MagickPathExtent];

-  assert(image_info != (ImageInfo *) NULL);
-  assert(image_info->signature == MagickCoreSignature);
-  assert(exception != (ExceptionInfo *) NULL);
-  if (CheckCoderDepth(image_info,exception) == MagickFalse)
-    return((Image *) NULL);
   *magic='\0';
   (void) GetPathComponent(image_info->filename,MagickPath,magic);
   if (*magic != '\0')
@@ -100,21 +110,49 @@ static inline Image *StrictReadImage(ImageInfo *image_info,
   return(ReadImage(image_info,exception));
 }

+static inline Image *StrictReadImage(ImageInfo *image_info,
+  ExceptionInfo *exception)
+{
+  Image
+    *image;
+
+  assert(image_info != (ImageInfo *) NULL);
+  assert(image_info->signature == MagickCoreSignature);
+  assert(exception != (ExceptionInfo *) NULL);
+  if (CheckCoderDepth(image_info,exception) == MagickFalse)
+    {
+      ReleaseCoderDepth(image_info);
+      return((Image *) NULL);
+    }
+  image=StrictReadImageInternal(image_info,exception);
+  ReleaseCoderDepth(image_info);
+  return(image);
+}
+
 static inline Image *StrictReadInlineImage(ImageInfo *image_info,
   const char *content,ExceptionInfo *exception)
 {
+  Image
+    *image;
+
   assert(image_info != (ImageInfo *) NULL);
   assert(image_info->signature == MagickCoreSignature);
   assert(exception != (ExceptionInfo *) NULL);
   if (CheckCoderDepth(image_info,exception) == MagickFalse)
-    return((Image *) NULL);
+    {
+      ReleaseCoderDepth(image_info);
+      return((Image *) NULL);
+    }
   if (content == (const char *) NULL)
     {
+      ReleaseCoderDepth(image_info);
       (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
         "NullInlineImageContent","`%s'",image_info->filename);
       return((Image *) NULL);
     }
-  return(ReadInlineImage(image_info,content,exception));
+  image=ReadInlineImage(image_info,content,exception);
+  ReleaseCoderDepth(image_info);
+  return(image);
 }

 #if defined(__cplusplus) || defined(c_plusplus)
diff --git a/MagickCore/constitute.c b/MagickCore/constitute.c
index 8cde5e05e..da0e963c6 100644
--- a/MagickCore/constitute.c
+++ b/MagickCore/constitute.c
@@ -318,7 +318,10 @@ MagickExport Image *PingImage(const ImageInfo *image_info,
   if (image != (Image *) NULL)
     {
       if ((image->columns == 0) || (image->rows == 0))
-        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+        {
+          ping_info=DestroyImageInfo(ping_info);
+          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+        }
       ResetTimer(&image->timer);
       if (ping_info->verbose != MagickFalse)
         (void) IdentifyImage(image,stdout,MagickFalse,exception);
@@ -483,6 +486,7 @@ static void InitializeConstituteInfo(const ImageInfo *image_info,
     constitute_info->sync_from_tiff=MagickFalse;
   constitute_info->caption=GetImageOption(image_info,"caption");
   constitute_info->comment=GetImageOption(image_info,"comment");
+  constitute_info->dispose=GetImageOption(image_info,"dispose");
   constitute_info->label=GetImageOption(image_info,"label");
   option=GetImageOption(image_info,"delay");
   if (option != (const char *) NULL)
@@ -656,6 +660,15 @@ static Image *ReadImageInternal(const ImageInfo *image_info,
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
   read_info=CloneImageInfo(image_info);
+  if (CheckCoderDepth(read_info,exception) == MagickFalse)
+    {
+      /*
+        Coders that read nested images (e.g. via a cloned image_info) are
+        bounded; read_info is private so the bump needs no release.
+      */
+      read_info=DestroyImageInfo(read_info);
+      return((Image *) NULL);
+    }
   (void) CopyMagickString(magick_filename,read_info->filename,MagickPathExtent);
   (void) SetImageInfo(read_info,0,exception);
   (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
@@ -681,6 +694,11 @@ static Image *ReadImageInternal(const ImageInfo *image_info,
       (GetMagickDecoderSeekableStream(magick_info) != MagickFalse))
     {
       image=AcquireImage(read_info,exception);
+      if (image == (Image *) NULL)
+        {
+          read_info=DestroyImageInfo(read_info);
+          return((Image *) NULL);
+        }
       (void) CopyMagickString(image->filename,read_info->filename,
         MagickPathExtent);
       status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
@@ -757,6 +775,8 @@ static Image *ReadImageInternal(const ImageInfo *image_info,
       image=AcquireImage(read_info,exception);
       if (image == (Image *) NULL)
         {
+          if (read_info->temporary != MagickFalse)
+            (void) RelinquishUniqueFileResource(read_info->filename);
           read_info=DestroyImageInfo(read_info);
           return((Image *) NULL);
         }
@@ -784,6 +804,8 @@ static Image *ReadImageInternal(const ImageInfo *image_info,
           else
             ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
               read_info->filename);
+          if (read_info->temporary != MagickFalse)
+            (void) RelinquishUniqueFileResource(read_info->filename);
           read_info=DestroyImageInfo(read_info);
           return((Image *) NULL);
         }
@@ -826,7 +848,7 @@ static Image *ReadImageInternal(const ImageInfo *image_info,
     {
       for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
       {
-        if ((image->columns == 0) || (image->rows == 0))
+        if ((next->columns == 0) || (next->rows == 0))
           {
             read_info=DestroyImageInfo(read_info);
             ThrowReaderException(ImageError,"NegativeOrZeroImageSize");
@@ -857,9 +879,6 @@ static Image *ReadImageInternal(const ImageInfo *image_info,
       magick_path[MagickPathExtent],
       *property;

-    const StringInfo
-      *profile;
-
     next->taint=MagickFalse;
     GetPathComponent(magick_filename,MagickPath,magick_path);
     if ((*magick_path == '\0') && (*next->magick == '\0'))
@@ -945,12 +964,6 @@ static Image *ReadImageInternal(const ImageInfo *image_info,
                 }
           }
       }
-    profile=GetImageProfile(next,"icc");
-    if (profile == (const StringInfo *) NULL)
-      profile=GetImageProfile(next,"icm");
-    profile=GetImageProfile(next,"iptc");
-    if (profile == (const StringInfo *) NULL)
-      profile=GetImageProfile(next,"8bim");
     if (IsSourceDataEpochSet() == MagickFalse)
       {
         char
@@ -1245,12 +1258,17 @@ MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
   /*
     Skip over header (e.g. data:image/gif;base64,).
   */
+  assert(image_info != (const ImageInfo *) NULL);
+  assert(image_info->signature == MagickCoreSignature);
+  assert(exception != (ExceptionInfo *) NULL);
   image=NewImageList();
+  if (content == (const char *) NULL)
+    ThrowReaderException(OptionError,"NullInlineImageContent");
   for (p=content; (*p != ',') && (*p != '\0'); p++) ;
   if (*p == '\0')
     ThrowReaderException(CorruptImageError,"CorruptImage");
   blob=Base64Decode(++p,&length);
-  if (length == 0)
+  if ((blob == (unsigned char *) NULL) || (length == 0))
     {
       blob=(unsigned char *) RelinquishMagickMemory(blob);
       ThrowReaderException(CorruptImageError,"CorruptImage");
@@ -1614,11 +1632,11 @@ MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  write_info=CloneImageInfo(image_info);
-  *write_info->magick='\0';
   images=GetFirstImageInList(images);
   if (images == (Image *) NULL)
     return(MagickFalse);
+  write_info=CloneImageInfo(image_info);
+  *write_info->magick='\0';
   if (filename != (const char *) NULL)
     for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
       (void) CopyMagickString(p->filename,filename,MagickPathExtent);
diff --git a/MagickCore/delegate-private.h b/MagickCore/delegate-private.h
index ab23cffae..d6d04fb9a 100644
--- a/MagickCore/delegate-private.h
+++ b/MagickCore/delegate-private.h
@@ -133,7 +133,10 @@ static inline void FormatSanitizedDelegateOption(char *string,
     *sanitized_option;

   magick_unreferenced(non_windows_format);
+  *string='\0';
   sanitized_option=SanitizeDelegateString(option);
+  if (sanitized_option == (char *) NULL)
+    return;
   (void) FormatLocaleString(string,length,windows_format,sanitized_option);
   sanitized_option=DestroyString(sanitized_option);
 }
@@ -146,7 +149,10 @@ static inline void FormatSanitizedDelegateOption(char *string,
     *sanitized_option;

   magick_unreferenced(windows_format);
+  *string='\0';
   sanitized_option=SanitizeDelegateString(option);
+  if (sanitized_option == (char *) NULL)
+    return;
   (void) FormatLocaleString(string,length,non_windows_format,sanitized_option);
   sanitized_option=DestroyString(sanitized_option);
 }
diff --git a/MagickCore/delegate.c b/MagickCore/delegate.c
index de70ea825..06fd802cd 100644
--- a/MagickCore/delegate.c
+++ b/MagickCore/delegate.c
@@ -109,8 +109,8 @@ static const char
     "  <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\"" DELEGATE_ESC "xdg-open" DELEGATE_ESC " https://imagemagick.org/; rm " DELEGATE_ESC "%i" DELEGATE_ESC "\"/>"
     "  <delegate decode=\"cdr\" command=\"" DELEGATE_ESC "uniconvertor" DELEGATE_ESC " " DELEGATE_ESC "%i" DELEGATE_ESC " " DELEGATE_ESC "%o.svg" DELEGATE_ESC "; mv " DELEGATE_ESC "%o.svg" DELEGATE_ESC " " DELEGATE_ESC "%o" DELEGATE_ESC "\"/>"
     "  <delegate decode=\"cgm\" command=\"" DELEGATE_ESC "uniconvertor" DELEGATE_ESC " " DELEGATE_ESC "%i" DELEGATE_ESC " " DELEGATE_ESC "%o.svg" DELEGATE_ESC "; mv " DELEGATE_ESC "%o.svg" DELEGATE_ESC " " DELEGATE_ESC "%o" DELEGATE_ESC "\"/>"
-    "  <delegate decode=\"http:decode\" command=\"" DELEGATE_ESC "curl" DELEGATE_ESC " -s -L -o " DELEGATE_ESC "%u.dat" DELEGATE_ESC " " DELEGATE_ESC "http:%M" DELEGATE_ESC "\"/>"
-    "  <delegate decode=\"https:decode\" command=\"" DELEGATE_ESC "curl" DELEGATE_ESC " -s -L -o " DELEGATE_ESC "%u.dat" DELEGATE_ESC " " DELEGATE_ESC "https:%M" DELEGATE_ESC "\"/>"
+    "  <delegate decode=\"http:decode\" command=\"" DELEGATE_ESC "curl" DELEGATE_ESC " -s -L --proto =http,https --proto-redir =http,https -o " DELEGATE_ESC "%u.dat" DELEGATE_ESC " " DELEGATE_ESC "http:%M" DELEGATE_ESC "\"/>"
+    "  <delegate decode=\"https:decode\" command=\"" DELEGATE_ESC "curl" DELEGATE_ESC " -s -L --proto =http,https --proto-redir =http,https -o " DELEGATE_ESC "%u.dat" DELEGATE_ESC " " DELEGATE_ESC "https:%M" DELEGATE_ESC "\"/>"
     "  <delegate decode=\"doc\" command=\"" DELEGATE_ESC "soffice" DELEGATE_ESC " --convert-to pdf -outdir `dirname " DELEGATE_ESC "%i" DELEGATE_ESC "` " DELEGATE_ESC "%i" DELEGATE_ESC " 2&gt; " DELEGATE_ESC "%u" DELEGATE_ESC "; mv " DELEGATE_ESC "%i.pdf" DELEGATE_ESC " " DELEGATE_ESC "%o" DELEGATE_ESC "\"/>"
     "  <delegate decode=\"docx\" command=\"" DELEGATE_ESC "soffice" DELEGATE_ESC " --convert-to pdf -outdir `dirname " DELEGATE_ESC "%i" DELEGATE_ESC "` " DELEGATE_ESC "%i" DELEGATE_ESC " 2&gt; " DELEGATE_ESC "%u" DELEGATE_ESC "; mv " DELEGATE_ESC "%i.pdf" DELEGATE_ESC " " DELEGATE_ESC "%o" DELEGATE_ESC "\"/>"
     "  <delegate decode=\"dng:decode\" command=\"" DELEGATE_ESC "ufraw-batch" DELEGATE_ESC " --silent --create-id=also --out-type=png --out-depth=16 " DELEGATE_ESC "--output=%u.png" DELEGATE_ESC " " DELEGATE_ESC "%i" DELEGATE_ESC "\"/>"
@@ -449,8 +449,23 @@ MagickExport int ExternalDelegateCommand(const MagickBooleanType asynchronous,
       (void) fflush(stderr);
     }
   sanitize_command=SanitizeString(command);
+  if (sanitize_command == (char *) NULL)
+    {
+      for (i=0; i < (ssize_t) number_arguments; i++)
+        arguments[i]=DestroyString(arguments[i]);
+      arguments=(char **) RelinquishMagickMemory(arguments);
+      return(-1);
+    }
   if (asynchronous != MagickFalse)
-    (void) ConcatenateMagickString(sanitize_command,"&",MagickPathExtent);
+    {
+      /*
+        SanitizeString() allocates strlen()+MagickPathExtent, so there is
+        always room; bounding by MagickPathExtent silently dropped the '&'
+        (running the command synchronously) for long commands.
+      */
+      (void) ConcatenateMagickString(sanitize_command,"&",
+        strlen(sanitize_command)+2);
+    }
   if (message != (char *) NULL)
     *message='\0';
 #if defined(MAGICKCORE_POSIX_SUPPORT)
@@ -1189,7 +1204,8 @@ MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   delegate_info=GetDelegateInfo(decode,encode,exception);
-  if (delegate_info == (const DelegateInfo *) NULL)
+  if ((delegate_info == (const DelegateInfo *) NULL) ||
+      (delegate_info->commands == (char *) NULL))
     {
       (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
         "NoTagFound","`%s'",decode ? decode : encode);
@@ -1517,7 +1533,11 @@ MagickExport char **GetDelegateList(const char *pattern,
   *number_delegates=0;
   if (IsDelegateCacheInstantiated(exception) == MagickFalse)
     return((char **) NULL);
-  delegates=(char **) AcquireQuantumMemory((size_t)
+  /*
+    Each delegate can contribute both its decode and its encode tag, so the
+    list needs room for two entries per delegate plus the terminator.
+  */
+  delegates=(char **) AcquireQuantumMemory((size_t) 2*
     GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
   if (delegates == (char **) NULL)
     return((char **) NULL);
@@ -1530,9 +1550,11 @@ MagickExport char **GetDelegateList(const char *pattern,

     delegate_info=(const DelegateInfo *) p->value;
     if ((delegate_info->stealth == MagickFalse) &&
-       (GlobExpression(delegate_info->decode,pattern,MagickFalse) != MagickFalse))
+        (delegate_info->decode != (char *) NULL) &&
+        (GlobExpression(delegate_info->decode,pattern,MagickFalse) != MagickFalse))
       delegates[i++]=ConstantString(delegate_info->decode);
     if ((delegate_info->stealth == MagickFalse) &&
+        (delegate_info->encode != (char *) NULL) &&
         (GlobExpression(delegate_info->encode,pattern,MagickFalse) != MagickFalse))
       delegates[i++]=ConstantString(delegate_info->encode);
     p=p->next;
@@ -1714,18 +1736,13 @@ static MagickBooleanType CopyDelegateFile(const char *source,
       (GetPathAttributes(destination,&attributes) != MagickFalse))
     return(MagickTrue);
   if (IsPathAuthorized(WritePolicyRights,destination) == MagickFalse)
+    ThrowPolicyException(destination,MagickFalse);
+  if (IsPathAuthorized(ReadPolicyRights,source) == MagickFalse)
     ThrowPolicyException(source,MagickFalse);
   destination_file=open_utf8(destination,O_WRONLY | O_CLOEXEC | O_BINARY |
     O_CREAT | O_NOFOLLOW,S_MODE);
   if (destination_file == -1)
     return(MagickFalse);
-  if (IsPathAuthorized(WritePolicyRights,destination) == MagickFalse)
-    ThrowPolicyException(destination,MagickFalse);
-  if (IsPathAuthorized(ReadPolicyRights,source) == MagickFalse)
-    {
-      (void) close_utf8(destination_file);
-      ThrowPolicyException(source,MagickFalse);
-    }
   source_file=open_utf8(source,O_RDONLY | O_CLOEXEC | O_BINARY,0);
   if (source_file == -1)
     {
@@ -1842,7 +1859,8 @@ MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
       return(MagickFalse);
     }
   delegate_info=GetDelegateInfo(decode,encode,exception);
-  if (delegate_info == (DelegateInfo *) NULL)
+  if ((delegate_info == (DelegateInfo *) NULL) ||
+      (delegate_info->commands == (char *) NULL))
     {
       if (temporary != MagickFalse)
         (void) RelinquishUniqueFileResource(image->filename);
@@ -1862,7 +1880,8 @@ MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
         }
       image_info->temporary=MagickTrue;
     }
-  if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
+  if ((delegate_info->mode != 0) && (delegate_info->decode != (char *) NULL) &&
+      (((decode != (const char *) NULL) &&
         (delegate_info->encode != (char *) NULL)) ||
        ((encode != (const char *) NULL) &&
         (delegate_info->decode != (char *) NULL))))
@@ -1881,6 +1900,8 @@ MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
       */
       if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
         {
+          if (temporary != MagickFalse)
+            (void) RelinquishUniqueFileResource(image->filename);
           ThrowFileException(exception,FileOpenError,
             "UnableToCreateTemporaryFile",image_info->unique);
           return(MagickFalse);
@@ -1955,6 +1976,7 @@ MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
     (void) AcquireUniqueSymbolicLink(output_filename,image_info->filename);
     if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
       {
+        status=MagickFalse;
         ThrowFileException(exception,FileOpenError,
           "UnableToCreateTemporaryFile",image_info->unique);
         break;
@@ -1971,7 +1993,9 @@ MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
       }
     status=MagickTrue;
     command=InterpretDelegateProperties(image_info,image,commands[i],exception);
-    if (command != (char *) NULL)
+    if (command == (char *) NULL)
+      status=MagickFalse;
+    else
       {
         /*
           Execute delegate.
@@ -2269,6 +2293,14 @@ static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
         /*
           Delegate element.
         */
+        if (delegate_info != (DelegateInfo *) NULL)
+          {
+            /*
+              Unterminated element: discard it rather than leak it.
+            */
+            (void) DestroyDelegate(delegate_info);
+            delegate_info=(DelegateInfo *) NULL;
+          }
         delegate_info=(DelegateInfo *) AcquireCriticalMemory(
           sizeof(*delegate_info));
         (void) memset(delegate_info,0,sizeof(*delegate_info));
@@ -2280,13 +2312,28 @@ static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
     if (delegate_info == (DelegateInfo *) NULL)
       continue;
     if ((LocaleCompare(keyword,"/>") == 0) ||
-        (LocaleCompare(keyword,"</policy>") == 0))
+        (LocaleCompare(keyword,"</delegate>") == 0))
       {
-        status=AppendValueToLinkedList(cache,delegate_info);
-        if (status == MagickFalse)
-          (void) ThrowMagickException(exception,GetMagickModule(),
-            ResourceLimitError,"MemoryAllocationFailed","`%s'",
-            delegate_info->commands);
+        if (delegate_info->commands == (char *) NULL)
+          {
+            /*
+              A delegate without a command cannot be invoked; reject it here
+              so no caller hands a NULL command list to StringToList().
+            */
+            (void) ThrowMagickException(exception,GetMagickModule(),
+              ConfigureWarning,"DelegateMissingCommand","`%s'",filename);
+            (void) DestroyDelegate(delegate_info);
+            delegate_info=(DelegateInfo *) NULL;
+            continue;
+          }
+        if (AppendValueToLinkedList(cache,delegate_info) == MagickFalse)
+          {
+            status=MagickFalse;
+            (void) ThrowMagickException(exception,GetMagickModule(),
+              ResourceLimitError,"MemoryAllocationFailed","`%s'",
+              delegate_info->commands);
+            (void) DestroyDelegate(delegate_info);
+          }
         delegate_info=(DelegateInfo *) NULL;
         continue;
       }
@@ -2335,6 +2382,8 @@ static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
       {
         if (LocaleCompare((char *) keyword,"decode") == 0)
           {
+            if (delegate_info->decode != (char *) NULL)
+              delegate_info->decode=DestroyString(delegate_info->decode);
             delegate_info->decode=ConstantString(token);
             delegate_info->mode=1;
             break;
@@ -2346,6 +2395,8 @@ static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
       {
         if (LocaleCompare((char *) keyword,"encode") == 0)
           {
+            if (delegate_info->encode != (char *) NULL)
+              delegate_info->encode=DestroyString(delegate_info->encode);
             delegate_info->encode=ConstantString(token);
             delegate_info->mode=(-1);
             break;
@@ -2388,7 +2439,8 @@ static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
         if (LocaleCompare((char *) keyword,"thread-support") == 0)
           {
             delegate_info->thread_support=IsStringTrue(token);
-            if (delegate_info->thread_support == MagickFalse)
+            if ((delegate_info->thread_support == MagickFalse) &&
+                (delegate_info->semaphore == (SemaphoreInfo *) NULL))
               delegate_info->semaphore=AcquireSemaphoreInfo();
             break;
           }
@@ -2398,6 +2450,8 @@ static MagickBooleanType LoadDelegateCache(LinkedListInfo *cache,
         break;
     }
   }
+  if (delegate_info != (DelegateInfo *) NULL)
+    (void) DestroyDelegate(delegate_info);  /* unterminated last element */
   token=(char *) RelinquishMagickMemory(token);
   return(status);
 }