Commit 94bea84eb for imagemagick.org

commit 94bea84eb33a6a043a09529cd88310264f2fd2dc
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat May 30 12:55:48 2026 -0400

    fix rare exception message leak

diff --git a/MagickCore/distribute-cache.c b/MagickCore/distribute-cache.c
index 6e5466bca..a12b7e5f2 100644
--- a/MagickCore/distribute-cache.c
+++ b/MagickCore/distribute-cache.c
@@ -330,6 +330,7 @@ static int ConnectPixelCacheServer(const char *hostname,const int port,
 {
 #if defined(MAGICKCORE_HAVE_DISTRIBUTE_CACHE)
   char
+    *message,
     service[MagickPathExtent],
     *shared_secret;

@@ -373,8 +374,10 @@ static int ConnectPixelCacheServer(const char *hostname,const int port,
   if (client_socket == -1)
     {
       freeaddrinfo(result);
+      message=GetExceptionMessage(errno);
       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
-        "DistributedPixelCache","'%s': %s",hostname,GetExceptionMessage(errno));
+        "DistributedPixelCache","'%s': %s",hostname,message);
+      message=DestroyString(message);
       return(-1);
     }
   status=connect(client_socket,result->ai_addr,(socklen_t) result->ai_addrlen);
@@ -382,8 +385,10 @@ static int ConnectPixelCacheServer(const char *hostname,const int port,
   if (status == -1)
     {
       CLOSE_SOCKET(client_socket);
+      message=GetExceptionMessage(errno);
       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
-        "DistributedPixelCache","'%s': %s",hostname,GetExceptionMessage(errno));
+        "DistributedPixelCache","'%s': %s",hostname,message);
+      message=DestroyString(message);
       return(-1);
     }
   /*
@@ -393,8 +398,10 @@ static int ConnectPixelCacheServer(const char *hostname,const int port,
   if (count != (ssize_t) sizeof(nonce))
     {
       CLOSE_SOCKET(client_socket);
+      message=GetExceptionMessage(errno);
       (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
-        "DistributedPixelCache","'%s': %s",hostname,GetExceptionMessage(errno));
+        "DistributedPixelCache","'%s': %s",hostname,message);
+      message=DestroyString(message);
       return(-1);
     }
   /*
diff --git a/MagickCore/statistic-private.h b/MagickCore/statistic-private.h
index f5be43026..b10e0e923 100644
--- a/MagickCore/statistic-private.h
+++ b/MagickCore/statistic-private.h
@@ -22,8 +22,6 @@
 extern "C" {
 #endif

-#define MagickEpsilonHuLog 1.0e-30
-
 static inline MagickBooleanType MagickSafeSignificantError(const double error,
   const double fuzz)
 {
@@ -31,11 +29,15 @@ static inline MagickBooleanType MagickSafeSignificantError(const double error,
   return(error > threshold ? MagickTrue : MagickFalse);
 }

+#define MagickLogEpsilon 1.0e-30
+
 static inline double MagickSafeLog10(const double x)
 {
-  if (x < MagickEpsilonHuLog)
-    return(log10(MagickEpsilonHuLog));
-  if (fabs(x-1.0) < MagickEpsilonHuLog)
+  if (x <= 0.0)
+    return(log10(MagickLogEpsilon));
+  if (x < MagickLogEpsilon)
+    return(log10(MagickLogEpsilon));
+  if (fabs(x-1.0) < MagickLogEpsilon)
     return(0.0);
   return(log10(x));
 }
diff --git a/MagickCore/statistic.c b/MagickCore/statistic.c
index 6eb7d4787..391d03285 100644
--- a/MagickCore/statistic.c
+++ b/MagickCore/statistic.c
@@ -328,7 +328,7 @@ static double ApplyEvaluateOperator(RandomInfo *random_info,const Quantum pixel,
     }
     case LogEvaluateOperator:
     {
-      if ((QuantumScale*(double) pixel) >= MagickEpsilonHuLog)
+      if ((QuantumScale*(double) pixel) >= MagickLogEpsilon)
         result=(double) QuantumRange*log(QuantumScale*value*
           (double) pixel+1.0)/log((double) (value+1.0));
       break;
diff --git a/MagickWand/compare.c b/MagickWand/compare.c
index 25c2dc2b7..e4dcd6260 100644
--- a/MagickWand/compare.c
+++ b/MagickWand/compare.c
@@ -221,9 +221,10 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
 }
 #define ThrowCompareException(asperity,tag,option) \
 { \
-  if (exception->severity < (asperity)) \
-    (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
-      "`%s'",option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyCompare(); \
   return(MagickFalse); \
 }
@@ -325,7 +326,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowCompareException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=1; i < ((ssize_t) argc-1); i++)
   {
     option=argv[i];
@@ -1610,7 +1611,7 @@ WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info,
             exception);
           if (text == (char *) NULL)
             ThrowCompareException(ResourceLimitError,"MemoryAllocationFailed",
-              GetExceptionMessage(errno));
+              (char *) NULL);
           (void) ConcatenateString(&(*metadata),text);
           text=DestroyString(text);
         }
diff --git a/MagickWand/composite.c b/MagickWand/composite.c
index 496800193..d0d313280 100644
--- a/MagickWand/composite.c
+++ b/MagickWand/composite.c
@@ -422,8 +422,10 @@ WandExport MagickBooleanType CompositeImageCommand(ImageInfo *image_info,
 }
 #define ThrowCompositeException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-     option == (char *) NULL ? GetExceptionMessage(errno) : option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyComposite(); \
   return(MagickFalse); \
 }
@@ -514,7 +516,7 @@ WandExport MagickBooleanType CompositeImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowCompositeException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=1; i < ((ssize_t) argc-1); i++)
   {
     option=argv[i];
@@ -1688,7 +1690,7 @@ WandExport MagickBooleanType CompositeImageCommand(ImageInfo *image_info,
       text=InterpretImageProperties(image_info,images,format,exception);
       if (text == (char *) NULL)
         ThrowCompositeException(ResourceLimitError,"MemoryAllocationFailed",
-          GetExceptionMessage(errno));
+          (char *) NULL);
       (void) ConcatenateString(&(*metadata),text);
       text=DestroyString(text);
     }
diff --git a/MagickWand/conjure.c b/MagickWand/conjure.c
index 1ca672277..9c5fc8560 100644
--- a/MagickWand/conjure.c
+++ b/MagickWand/conjure.c
@@ -128,8 +128,10 @@ WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info,
 }
 #define ThrowConjureException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-     option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyConjure(); \
   return(MagickFalse); \
 }
@@ -184,7 +186,7 @@ WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=1; i < (ssize_t) argc; i++)
   {
     option=argv[i];
diff --git a/MagickWand/deprecate.c b/MagickWand/deprecate.c
index af6966da2..8d5bcf385 100644
--- a/MagickWand/deprecate.c
+++ b/MagickWand/deprecate.c
@@ -502,8 +502,10 @@ WandExport MagickBooleanType ConvertImageCommand(ImageInfo *image_info,
 }
 #define ThrowConvertException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-    option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyConvert(); \
   return(MagickFalse); \
 }
@@ -589,7 +591,7 @@ WandExport MagickBooleanType ConvertImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowConvertException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   if ((argc > 2) && (LocaleCompare("-concatenate",argv[1]) == 0))
     return(ConcatenateImages(argc,argv,exception));
   for (i=1; i < ((ssize_t) argc-1); i++)
@@ -3380,7 +3382,7 @@ WandExport MagickBooleanType ConvertImageCommand(ImageInfo *image_info,
       text=InterpretImageProperties(image_info,image,format,exception);
       if (text == (char *) NULL)
         ThrowConvertException(ResourceLimitError,"MemoryAllocationFailed",
-          GetExceptionMessage(errno));
+          (char *) NULL);
       (void) ConcatenateString(&(*metadata),text);
       text=DestroyString(text);
     }
diff --git a/MagickWand/display.c b/MagickWand/display.c
index 7ff85677c..3eb980b57 100644
--- a/MagickWand/display.c
+++ b/MagickWand/display.c
@@ -264,8 +264,10 @@ WandExport MagickBooleanType DisplayImageCommand(ImageInfo *image_info,
 }
 #define ThrowDisplayException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-    option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyDisplay(); \
   return(MagickFalse); \
 }
@@ -367,12 +369,12 @@ WandExport MagickBooleanType DisplayImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowDisplayException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   image_marker=(size_t *) AcquireQuantumMemory((size_t) argc+1UL,
     sizeof(*image_marker));
   if (image_marker == (size_t *) NULL)
     ThrowDisplayException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=0; i <= (ssize_t) argc; i++)
     image_marker[i]=(size_t) argc;
   /*
@@ -503,7 +505,7 @@ WandExport MagickBooleanType DisplayImageCommand(ImageInfo *image_info,
         image_list=CloneImageList(image,exception);
         if (image_list == (Image *) NULL)
           ThrowDisplayException(ResourceLimitError,"MemoryAllocationFailed",
-            GetExceptionMessage(errno));
+            (char *) NULL);
         display_image=image_list;
         do
         {
diff --git a/MagickWand/drawing-wand.c b/MagickWand/drawing-wand.c
index 059cb8260..a94659a5e 100644
--- a/MagickWand/drawing-wand.c
+++ b/MagickWand/drawing-wand.c
@@ -485,8 +485,8 @@ WandExport DrawingWand *CloneDrawingWand(const DrawingWand *wand)
     (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name);
   clone_wand=(DrawingWand *) AcquireMagickMemory(sizeof(*clone_wand));
   if (clone_wand == (DrawingWand *) NULL)
-    ThrowWandFatalException(ResourceLimitFatalError,
-      "MemoryAllocationFailed",GetExceptionMessage(errno));
+    ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
+      (char *) NULL);
   (void) memset(clone_wand,0,sizeof(*clone_wand));
   clone_wand->id=AcquireWandId();
   (void) FormatLocaleString(clone_wand->name,MagickPathExtent,
@@ -505,7 +505,7 @@ WandExport DrawingWand *CloneDrawingWand(const DrawingWand *wand)
     wand->index+1UL,sizeof(*wand->graphic_context));
   if (clone_wand->graphic_context == (DrawInfo **) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=0; i <= (ssize_t) wand->index; i++)
     clone_wand->graphic_context[i]=CloneDrawInfo((ImageInfo *) NULL,
       wand->graphic_context[i]);
@@ -6779,7 +6779,7 @@ WandExport DrawingWand *NewDrawingWand(void)
   wand=(DrawingWand *) AcquireMagickMemory(sizeof(*wand));
   if (wand == (DrawingWand *) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   (void) memset(wand,0,sizeof(*wand));
   wand->id=AcquireWandId();
   (void) FormatLocaleString(wand->name,MagickPathExtent,"%s-%.20g",
@@ -6801,7 +6801,7 @@ WandExport DrawingWand *NewDrawingWand(void)
     *wand->graphic_context));
   if (wand->graphic_context == (DrawInfo **) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   wand->filter_off=MagickTrue;
   wand->indent_depth=0;
   wand->path_operation=PathDefaultOperation;
diff --git a/MagickWand/identify.c b/MagickWand/identify.c
index f63f67a75..e53c5cf63 100644
--- a/MagickWand/identify.c
+++ b/MagickWand/identify.c
@@ -181,8 +181,10 @@ WandExport MagickBooleanType IdentifyImageCommand(ImageInfo *image_info,
 }
 #define ThrowIdentifyException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-    option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyIdentify(); \
   return(MagickFalse); \
 }
@@ -263,7 +265,7 @@ WandExport MagickBooleanType IdentifyImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowIdentifyException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   image_info->ping=MagickTrue;
   for (i=1; i < (ssize_t) argc; i++)
   {
@@ -334,7 +336,7 @@ WandExport MagickBooleanType IdentifyImageCommand(ImageInfo *image_info,
               text=InterpretImageProperties(image_info,image,format,exception);
               if (text == (char *) NULL)
                 ThrowIdentifyException(ResourceLimitError,
-                  "MemoryAllocationFailed",GetExceptionMessage(errno));
+                  "MemoryAllocationFailed",(char *) NULL);
               (void) ConcatenateString(&(*metadata),text);
               text=DestroyString(text);
             }
diff --git a/MagickWand/import.c b/MagickWand/import.c
index 2cfc163e6..3af3db81b 100644
--- a/MagickWand/import.c
+++ b/MagickWand/import.c
@@ -218,8 +218,10 @@ WandExport MagickBooleanType ImportImageCommand(ImageInfo *image_info,
 }
 #define ThrowImportException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-     option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyImport(); \
   return(MagickFalse); \
 }
@@ -314,7 +316,7 @@ WandExport MagickBooleanType ImportImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowImportException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=1; i < (ssize_t) argc; i++)
   {
     /*
diff --git a/MagickWand/magick-cli.c b/MagickWand/magick-cli.c
index cf4afb8df..9e097cd7f 100644
--- a/MagickWand/magick-cli.c
+++ b/MagickWand/magick-cli.c
@@ -1254,7 +1254,7 @@ static MagickBooleanType ConcatenateImages(int argc,char **argv,

   if (ExpandFilenames(&argc,&argv) == MagickFalse)
     ThrowFileException(exception,ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      argv[argc-1]);
   output=fopen_utf8(argv[argc-1],"wb");
   if (output == (FILE *) NULL)
     {
@@ -1439,9 +1439,12 @@ Magick_Command_Exit:
       text=InterpretImageProperties(image_info,cli_wand->wand.images,format,
         exception);
       if (text == (char *) NULL)
-        (void) ThrowMagickException(exception,GetMagickModule(),
-          ResourceLimitError,"MemoryAllocationFailed","`%s'",
-          GetExceptionMessage(errno));
+        {
+          char *message = GetExceptionMessage(errno);
+          (void) ThrowMagickException(exception,GetMagickModule(),
+            ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
+          message=DestroyString(message);
+        }
       else
         {
           (void) ConcatenateString(&(*metadata),text);
diff --git a/MagickWand/magick-wand-private.h b/MagickWand/magick-wand-private.h
index 7c5d6e7c8..eb297de29 100644
--- a/MagickWand/magick-wand-private.h
+++ b/MagickWand/magick-wand-private.h
@@ -34,12 +34,11 @@ extern "C" {
 }
 #define ThrowWandFatalException(severity,tag,context) \
 { \
-  ExceptionInfo \
-    *fatal_exception; \
- \
-  fatal_exception=AcquireExceptionInfo(); \
+  ExceptionInfo *fatal_exception = AcquireExceptionInfo(); \
+  char *message = GetExceptionMessage(errno); \
   (void) ThrowMagickException(fatal_exception,GetMagickModule(),severity,tag, \
-    "`%s'",context); \
+    "`%s'",context == (char *) NULL ? message : context); \
+  message=DestroyString(message); \
   CatchException(fatal_exception); \
   (void) DestroyExceptionInfo(fatal_exception); \
   MagickWandTerminus(); \
diff --git a/MagickWand/magick-wand.c b/MagickWand/magick-wand.c
index 01e7d4adb..6ffcb68ee 100644
--- a/MagickWand/magick-wand.c
+++ b/MagickWand/magick-wand.c
@@ -1079,7 +1079,7 @@ WandExport MagickWand *NewMagickWand(void)
   wand=(MagickWand *) AcquireMagickMemory(sizeof(*wand));
   if (wand == (MagickWand *) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   (void) memset(wand,0,sizeof(*wand));
   wand->id=AcquireWandId();
   (void) FormatLocaleString(wand->name,MagickPathExtent,"%s-%.20g",MagickWandId,
diff --git a/MagickWand/mogrify.c b/MagickWand/mogrify.c
index 42580a3c5..f62076f12 100644
--- a/MagickWand/mogrify.c
+++ b/MagickWand/mogrify.c
@@ -3728,8 +3728,10 @@ WandExport MagickBooleanType MogrifyImageCommand(ImageInfo *image_info,
 }
 #define ThrowMogrifyException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-    option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyMogrify(); \
   return(MagickFalse); \
 }
@@ -3814,7 +3816,7 @@ WandExport MagickBooleanType MogrifyImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowMogrifyException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=1; i < (ssize_t) argc; i++)
   {
     option=argv[i];
diff --git a/MagickWand/montage.c b/MagickWand/montage.c
index e0daea708..011aba218 100644
--- a/MagickWand/montage.c
+++ b/MagickWand/montage.c
@@ -259,8 +259,10 @@ WandExport MagickBooleanType MontageImageCommand(ImageInfo *image_info,
 }
 #define ThrowMontageException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-    option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyMontage(); \
   return(MagickFalse); \
 }
@@ -358,7 +360,7 @@ WandExport MagickBooleanType MontageImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowMontageException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   for (i=1; i < ((ssize_t) argc-1); i++)
   {
     option=argv[i];
@@ -1864,7 +1866,7 @@ WandExport MagickBooleanType MontageImageCommand(ImageInfo *image_info,
             exception);
           if (text == (char *) NULL)
             ThrowMontageException(ResourceLimitError,"MemoryAllocationFailed",
-              GetExceptionMessage(errno));
+              (char *) NULL);
           (void) ConcatenateString(&(*metadata),text);
           text=DestroyString(text);
         }
diff --git a/MagickWand/operation.c b/MagickWand/operation.c
index 2246b980e..1ba586b6e 100644
--- a/MagickWand/operation.c
+++ b/MagickWand/operation.c
@@ -4776,7 +4776,7 @@ static MagickBooleanType CLIListOperatorImages(MagickCLI *cli_wand,
         swap=CloneImage(p,0,0,MagickTrue,_exception);
         if (swap == (Image *) NULL)
           CLIWandExceptArgBreak(ResourceLimitError,"MemoryAllocationFailed",
-            option,GetExceptionMessage(errno));
+            option,(char *) NULL);
         ReplaceImageInList(&p,CloneImage(q,0,0,MagickTrue,_exception));
         ReplaceImageInList(&q,swap);
         _images=GetFirstImageInList(q);
@@ -4947,7 +4947,7 @@ static void CLINoImageOperator(MagickCLI *cli_wand,
       */
       if (ExpandFilenames(&argc,&argv) == MagickFalse)
         CLIWandExceptArgBreak(ResourceLimitError,"MemoryAllocationFailed",
-            option,GetExceptionMessage(errno));
+          option,(char *) NULL);

       /* loop over expanded filename list, and read then all in */
       for (i=0; i < (ssize_t) argc; i++) {
diff --git a/MagickWand/stream.c b/MagickWand/stream.c
index 8baa05dee..6b2d4e47c 100644
--- a/MagickWand/stream.c
+++ b/MagickWand/stream.c
@@ -160,8 +160,10 @@ WandExport MagickBooleanType StreamImageCommand(ImageInfo *image_info,
 }
 #define ThrowStreamException(asperity,tag,option) \
 { \
-  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
-    option); \
+  char *message = GetExceptionMessage(errno);     \
+  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
+    "`%s'",option == (char *) NULL ? message : option); \
+  message=DestroyString(message); \
   DestroyStream(); \
   return(MagickFalse); \
 }
@@ -250,7 +252,7 @@ WandExport MagickBooleanType StreamImageCommand(ImageInfo *image_info,
   status=ExpandFilenames(&argc,&argv);
   if (status == MagickFalse)
     ThrowStreamException(ResourceLimitError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   status=OpenStream(image_info,stream_info,argv[argc-1],exception);
   if (status == MagickFalse)
     {
diff --git a/MagickWand/wand-view.c b/MagickWand/wand-view.c
index b26581352..478c54459 100644
--- a/MagickWand/wand-view.c
+++ b/MagickWand/wand-view.c
@@ -764,7 +764,7 @@ WandExport WandView *NewWandView(MagickWand *wand)
   wand_view->exception=exception;
   if (wand_view->pixel_wands == (PixelWand ***) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   wand_view->debug=IsEventLogging();
   wand_view->signature=MagickWandSignature;
   return(wand_view);
@@ -825,7 +825,7 @@ WandExport WandView *NewWandViewExtent(MagickWand *wand,const ssize_t x,
   wand_view->pixel_wands=AcquirePixelsTLS(wand_view->extent.width);
   if (wand_view->pixel_wands == (PixelWand ***) NULL)
     ThrowWandFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
-      GetExceptionMessage(errno));
+      (char *) NULL);
   wand_view->debug=IsEventLogging();
   wand_view->signature=MagickWandSignature;
   return(wand_view);
diff --git a/MagickWand/wandcli-private.h b/MagickWand/wandcli-private.h
index 52eeec359..56b5da756 100644
--- a/MagickWand/wandcli-private.h
+++ b/MagickWand/wandcli-private.h
@@ -27,8 +27,11 @@ extern "C" {
        "`%s'",option)

 #define CLIWandExceptionArg(severity,tag,option,arg) \
-  (void) CLIThrowException(cli_wand,GetMagickModule(),severity,tag, \
-       "'%s' '%s'",option, arg)
+  { \
+    char *message = GetExceptionMessage(errno); \
+    (void) CLIThrowException(cli_wand,GetMagickModule(),severity,tag, \
+      "'%s' '%s'",option, arg == (char *) NULL ? message : arg); \
+  }

 #define CLIWandWarnReplaced(message) \
   if ( (cli_wand->process_flags & ProcessWarnDeprecated) != 0 ) \
@@ -36,9 +39,10 @@ extern "C" {
        "ReplacedOption", "'%s', use \"%s\"",option,message)

 #define CLIWandExceptionFile(severity,tag,context) \
-{ char *message=GetExceptionMessage(errno); \
+{  \
+  char *message=GetExceptionMessage(errno); \
   (void) CLIThrowException(cli_wand,GetMagickModule(),severity,tag, \
-       "'%s': %s",context,message); \
+    "'%s': %s",context,message); \
   message=DestroyString(message); \
 }

@@ -49,13 +53,7 @@ extern "C" {
   { CLIWandException(severity,tag,option); return; }

 #define CLIWandExceptArgBreak(severity,tag,option,arg) \
-  {  \
-    if (errno != 0)  \
-      CLIWandExceptionArg(severity,tag,option,GetExceptionMessage(errno));  \
-    else  \
-      CLIWandExceptionArg(severity,tag,option,arg);  \
-    break; \
-  }
+  { CLIWandExceptionArg(severity,tag,option,arg); break; }

 #define CLIWandExceptArgReturn(severity,tag,option,arg) \
   { CLIWandExceptionArg(severity,tag,option,arg); return; }