2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE X X CCCC EEEEE PPPP TTTTT IIIII OOO N N %
7 % E X X C E P P T I O O NN N %
8 % EEE X C EEE PPPP T I O O N N N %
9 % E X X C E P T I O O N NN %
10 % EEEEE X X CCCC EEEEE P T IIIII OOO N N %
13 % MagickCore Exception Methods %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/linked-list.h"
48 #include "MagickCore/locale_.h"
49 #include "MagickCore/log.h"
50 #include "MagickCore/magick.h"
51 #include "MagickCore/memory_.h"
52 #include "MagickCore/memory-private.h"
53 #include "MagickCore/string_.h"
54 #include "MagickCore/utility.h"
55 #include "MagickCore/utility-private.h"
60 #define MaxExceptionList 64
65 #if defined(__cplusplus) || defined(c_plusplus)
70 DefaultErrorHandler(const ExceptionType,const char *,const char *),
71 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
72 DefaultWarningHandler(const ExceptionType,const char *,const char *);
74 #if defined(__cplusplus) || defined(c_plusplus)
82 error_handler = DefaultErrorHandler;
84 static FatalErrorHandler
85 fatal_error_handler = DefaultFatalErrorHandler;
88 warning_handler = DefaultWarningHandler;
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 % A c q u i r e E x c e p t i o n I n f o %
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % AcquireExceptionInfo() allocates the ExceptionInfo structure.
103 % The format of the AcquireExceptionInfo method is:
105 % ExceptionInfo *AcquireExceptionInfo(void)
108 MagickExport ExceptionInfo *AcquireExceptionInfo(void)
113 exception=(ExceptionInfo *) AcquireCriticalMemory(sizeof(*exception));
114 InitializeExceptionInfo(exception);
115 exception->relinquish=MagickTrue;
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % C l e a r M a g i c k E x c e p t i o n %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % ClearMagickException() clears any exception that may not have been caught
133 % The format of the ClearMagickException method is:
135 % ClearMagickException(ExceptionInfo *exception)
137 % A description of each parameter follows:
139 % o exception: the exception info.
143 static void *DestroyExceptionElement(void *exception)
145 register ExceptionInfo
148 p=(ExceptionInfo *) exception;
149 if (p->reason != (char *) NULL)
150 p->reason=DestroyString(p->reason);
151 if (p->description != (char *) NULL)
152 p->description=DestroyString(p->description);
153 p=(ExceptionInfo *) RelinquishMagickMemory(p);
154 return((void *) NULL);
157 MagickExport void ClearMagickException(ExceptionInfo *exception)
159 assert(exception != (ExceptionInfo *) NULL);
160 assert(exception->signature == MagickCoreSignature);
161 if (exception->exceptions == (void *) NULL)
163 LockSemaphoreInfo(exception->semaphore);
164 ClearLinkedList((LinkedListInfo *) exception->exceptions,
165 DestroyExceptionElement);
166 exception->severity=UndefinedException;
167 exception->reason=(char *) NULL;
168 exception->description=(char *) NULL;
169 UnlockSemaphoreInfo(exception->semaphore);
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 % C a t c h E x c e p t i o n %
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % CatchException() returns if no exceptions is found otherwise it reports
185 % the exception as a warning, error, or fatal depending on the severity.
187 % The format of the CatchException method is:
189 % CatchException(ExceptionInfo *exception)
191 % A description of each parameter follows:
193 % o exception: the exception info.
196 MagickExport void CatchException(ExceptionInfo *exception)
201 register const ExceptionInfo
207 assert(exception != (ExceptionInfo *) NULL);
208 assert(exception->signature == MagickCoreSignature);
209 if (exception->exceptions == (void *) NULL)
211 LockSemaphoreInfo(exception->semaphore);
212 exceptions=(LinkedListInfo *) exception->exceptions;
213 ResetLinkedListIterator(exceptions);
214 p=(const ExceptionInfo *) GetNextValueInLinkedList(exceptions);
215 for (i=0; p != (const ExceptionInfo *) NULL; i++)
217 if ((p->severity >= WarningException) && (p->severity < ErrorException))
218 MagickWarning(p->severity,p->reason,p->description);
219 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
220 MagickError(p->severity,p->reason,p->description);
221 if (p->severity >= FatalErrorException)
222 MagickFatalError(p->severity,p->reason,p->description);
223 p=(const ExceptionInfo *) GetNextValueInLinkedList(exceptions);
225 UnlockSemaphoreInfo(exception->semaphore);
226 ClearMagickException(exception);
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % C l o n e E x c e p t i o n I n f o %
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % CloneExceptionInfo() clones the ExceptionInfo structure.
242 % The format of the CloneExceptionInfo method is:
244 % ExceptionInfo *CloneException(ExceptionInfo *exception)
246 % A description of each parameter follows:
248 % o exception: the exception info.
251 MagickExport ExceptionInfo *CloneExceptionInfo(ExceptionInfo *exception)
256 clone_exception=(ExceptionInfo *) AcquireCriticalMemory(sizeof(*exception));
257 InitializeExceptionInfo(clone_exception);
258 InheritException(clone_exception,exception);
259 clone_exception->relinquish=MagickTrue;
260 return(clone_exception);
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 + D e f a u l t E r r o r H a n d l e r %
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % DefaultErrorHandler() displays an error reason.
276 % The format of the DefaultErrorHandler method is:
278 % void MagickError(const ExceptionType severity,const char *reason,
279 % const char *description)
281 % A description of each parameter follows:
283 % o severity: Specifies the numeric error category.
285 % o reason: Specifies the reason to display before terminating the
288 % o description: Specifies any description to the reason.
291 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
292 const char *reason,const char *description)
294 magick_unreferenced(severity);
296 if (reason == (char *) NULL)
298 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
299 if (description != (char *) NULL)
300 (void) FormatLocaleFile(stderr," (%s)",description);
301 (void) FormatLocaleFile(stderr,".\n");
302 (void) fflush(stderr);
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 + D e f a u l t F a t a l E r r o r H a n d l e r %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % DefaultFatalErrorHandler() displays an error reason and then terminates the
319 % The format of the DefaultFatalErrorHandler method is:
321 % void MagickFatalError(const ExceptionType severity,const char *reason,
322 % const char *description)
324 % A description of each parameter follows:
326 % o severity: Specifies the numeric error category.
328 % o reason: Specifies the reason to display before terminating the program.
330 % o description: Specifies any description to the reason.
333 static void DefaultFatalErrorHandler(const ExceptionType severity,
334 const char *reason,const char *description)
336 if (reason == (char *) NULL)
338 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
339 if (description != (char *) NULL)
340 (void) FormatLocaleFile(stderr," (%s)",description);
341 (void) FormatLocaleFile(stderr,".\n");
342 (void) fflush(stderr);
343 MagickCoreTerminus();
344 exit((int) (severity-FatalErrorException)+1);
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 + D e f a u l t W a r n i n g H a n d l e r %
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 % DefaultWarningHandler() displays a warning reason.
360 % The format of the DefaultWarningHandler method is:
362 % void DefaultWarningHandler(const ExceptionType severity,
363 % const char *reason,const char *description)
365 % A description of each parameter follows:
367 % o severity: Specifies the numeric warning category.
369 % o reason: Specifies the reason to display before terminating the
372 % o description: Specifies any description to the reason.
375 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
376 const char *reason,const char *description)
378 magick_unreferenced(severity);
380 if (reason == (char *) NULL)
382 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
383 if (description != (char *) NULL)
384 (void) FormatLocaleFile(stderr," (%s)",description);
385 (void) FormatLocaleFile(stderr,".\n");
386 (void) fflush(stderr);
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % D e s t r o y E x c e p t i o n I n f o %
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % DestroyExceptionInfo() deallocates memory associated with an exception.
402 % The format of the DestroyExceptionInfo method is:
404 % ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
406 % A description of each parameter follows:
408 % o exception: the exception info.
411 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
416 assert(exception != (ExceptionInfo *) NULL);
417 assert(exception->signature == MagickCoreSignature);
418 if (exception->semaphore == (SemaphoreInfo *) NULL)
419 ActivateSemaphoreInfo(&exception->semaphore);
420 LockSemaphoreInfo(exception->semaphore);
421 exception->severity=UndefinedException;
422 if (exception->relinquish != MagickFalse)
424 exception->signature=(~MagickCoreSignature);
425 if (exception->exceptions != (void *) NULL)
426 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
427 exception->exceptions,DestroyExceptionElement);
430 if (exception->exceptions != (void *) NULL)
431 ClearLinkedList((LinkedListInfo *) exception->exceptions,
432 DestroyExceptionElement);
433 relinquish=exception->relinquish;
434 UnlockSemaphoreInfo(exception->semaphore);
435 if (relinquish != MagickFalse)
437 RelinquishSemaphoreInfo(&exception->semaphore);
438 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 % G e t E x c e p t i o n M e s s a g e %
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 % GetExceptionMessage() returns the error message defined by the specified
457 % The format of the GetExceptionMessage method is:
459 % char *GetExceptionMessage(const int error)
461 % A description of each parameter follows:
463 % o error: the error code.
466 MagickExport char *GetExceptionMessage(const int error)
469 exception[MagickPathExtent];
472 #if defined(MAGICKCORE_HAVE_STRERROR_R)
473 #if !defined(MAGICKCORE_STRERROR_R_CHAR_P)
474 (void) strerror_r(error,exception,sizeof(exception));
476 (void) CopyMagickString(exception,strerror_r(error,exception,
477 sizeof(exception)),sizeof(exception));
480 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
482 return(ConstantString(exception));
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % G e t L o c a l e E x c e p t i o n M e s s a g e %
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 % GetLocaleExceptionMessage() converts a enumerated exception severity and tag
497 % to a message in the current locale.
499 % The format of the GetLocaleExceptionMessage method is:
501 % const char *GetLocaleExceptionMessage(const ExceptionType severity,
504 % A description of each parameter follows:
506 % o severity: the severity of the exception.
508 % o tag: the message tag.
512 static const char *ExceptionSeverityToTag(const ExceptionType severity)
516 case ResourceLimitWarning: return("Resource/Limit/Warning/");
517 case TypeWarning: return("Type/Warning/");
518 case OptionWarning: return("Option/Warning/");
519 case DelegateWarning: return("Delegate/Warning/");
520 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
521 case CorruptImageWarning: return("Corrupt/Image/Warning/");
522 case FileOpenWarning: return("File/Open/Warning/");
523 case BlobWarning: return("Blob/Warning/");
524 case StreamWarning: return("Stream/Warning/");
525 case CacheWarning: return("Cache/Warning/");
526 case CoderWarning: return("Coder/Warning/");
527 case FilterWarning: return("Filter/Warning/");
528 case ModuleWarning: return("Module/Warning/");
529 case DrawWarning: return("Draw/Warning/");
530 case ImageWarning: return("Image/Warning/");
531 case WandWarning: return("Wand/Warning/");
532 case XServerWarning: return("XServer/Warning/");
533 case MonitorWarning: return("Monitor/Warning/");
534 case RegistryWarning: return("Registry/Warning/");
535 case ConfigureWarning: return("Configure/Warning/");
536 case PolicyWarning: return("Policy/Warning/");
537 case ResourceLimitError: return("Resource/Limit/Error/");
538 case TypeError: return("Type/Error/");
539 case OptionError: return("Option/Error/");
540 case DelegateError: return("Delegate/Error/");
541 case MissingDelegateError: return("Missing/Delegate/Error/");
542 case CorruptImageError: return("Corrupt/Image/Error/");
543 case FileOpenError: return("File/Open/Error/");
544 case BlobError: return("Blob/Error/");
545 case StreamError: return("Stream/Error/");
546 case CacheError: return("Cache/Error/");
547 case CoderError: return("Coder/Error/");
548 case FilterError: return("Filter/Error/");
549 case ModuleError: return("Module/Error/");
550 case DrawError: return("Draw/Error/");
551 case ImageError: return("Image/Error/");
552 case WandError: return("Wand/Error/");
553 case XServerError: return("XServer/Error/");
554 case MonitorError: return("Monitor/Error/");
555 case RegistryError: return("Registry/Error/");
556 case ConfigureError: return("Configure/Error/");
557 case PolicyError: return("Policy/Error/");
558 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
559 case TypeFatalError: return("Type/FatalError/");
560 case OptionFatalError: return("Option/FatalError/");
561 case DelegateFatalError: return("Delegate/FatalError/");
562 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
563 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
564 case FileOpenFatalError: return("File/Open/FatalError/");
565 case BlobFatalError: return("Blob/FatalError/");
566 case StreamFatalError: return("Stream/FatalError/");
567 case CacheFatalError: return("Cache/FatalError/");
568 case CoderFatalError: return("Coder/FatalError/");
569 case FilterFatalError: return("Filter/FatalError/");
570 case ModuleFatalError: return("Module/FatalError/");
571 case DrawFatalError: return("Draw/FatalError/");
572 case ImageFatalError: return("Image/FatalError/");
573 case WandFatalError: return("Wand/FatalError/");
574 case XServerFatalError: return("XServer/FatalError/");
575 case MonitorFatalError: return("Monitor/FatalError/");
576 case RegistryFatalError: return("Registry/FatalError/");
577 case ConfigureFatalError: return("Configure/FatalError/");
578 case PolicyFatalError: return("Policy/FatalError/");
584 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
588 message[MagickPathExtent];
593 assert(tag != (const char *) NULL);
594 (void) FormatLocaleString(message,MagickPathExtent,"Exception/%s%s",
595 ExceptionSeverityToTag(severity),tag);
596 locale_message=GetLocaleMessage(message);
597 if (locale_message == (const char *) NULL)
599 if (locale_message == message)
601 return(locale_message);
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609 % I n h e r i t E x c e p t i o n %
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615 % InheritException() inherits an exception from a related exception.
617 % The format of the InheritException method is:
619 % InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
621 % A description of each parameter follows:
623 % o exception: the exception info.
625 % o relative: the related exception info.
628 MagickExport void InheritException(ExceptionInfo *exception,
629 const ExceptionInfo *relative)
631 register const ExceptionInfo
634 assert(exception != (ExceptionInfo *) NULL);
635 assert(exception->signature == MagickCoreSignature);
636 assert(relative != (ExceptionInfo *) NULL);
637 assert(relative->signature == MagickCoreSignature);
638 assert(exception != relative);
639 if (relative->exceptions == (void *) NULL)
641 LockSemaphoreInfo(relative->semaphore);
642 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
643 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
644 relative->exceptions);
645 while (p != (const ExceptionInfo *) NULL)
647 (void) ThrowException(exception,p->severity,p->reason,p->description);
648 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
649 relative->exceptions);
651 UnlockSemaphoreInfo(relative->semaphore);
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659 % I n i t i a l i z e t E x c e p t i o n I n f o %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665 % InitializeExceptionInfo() initializes an exception to default values.
667 % The format of the InitializeExceptionInfo method is:
669 % InitializeExceptionInfo(ExceptionInfo *exception)
671 % A description of each parameter follows:
673 % o exception: the exception info.
676 MagickPrivate void InitializeExceptionInfo(ExceptionInfo *exception)
678 assert(exception != (ExceptionInfo *) NULL);
679 (void) memset(exception,0,sizeof(*exception));
680 exception->severity=UndefinedException;
681 exception->exceptions=(void *) NewLinkedList(0);
682 exception->semaphore=AcquireSemaphoreInfo();
683 exception->signature=MagickCoreSignature;
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691 % M a g i c k E r r o r %
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
697 % MagickError() calls the exception handler methods with an error reason.
699 % The format of the MagickError method is:
701 % void MagickError(const ExceptionType error,const char *reason,
702 % const char *description)
704 % A description of each parameter follows:
706 % o exception: Specifies the numeric error category.
708 % o reason: Specifies the reason to display before terminating the
711 % o description: Specifies any description to the reason.
714 MagickExport void MagickError(const ExceptionType error,const char *reason,
715 const char *description)
717 if (error_handler != (ErrorHandler) NULL)
718 (*error_handler)(error,reason,description);
722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % M a g i c k F a t al E r r o r %
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 % MagickFatalError() calls the fatal exception handler methods with an error
735 % The format of the MagickError method is:
737 % void MagickFatalError(const ExceptionType error,const char *reason,
738 % const char *description)
740 % A description of each parameter follows:
742 % o exception: Specifies the numeric error category.
744 % o reason: Specifies the reason to display before terminating the
747 % o description: Specifies any description to the reason.
750 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
751 const char *description)
753 if (fatal_error_handler != (ErrorHandler) NULL)
754 (*fatal_error_handler)(error,reason,description);
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % M a g i c k W a r n i n g %
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 % MagickWarning() calls the warning handler methods with a warning reason.
770 % The format of the MagickWarning method is:
772 % void MagickWarning(const ExceptionType warning,const char *reason,
773 % const char *description)
775 % A description of each parameter follows:
777 % o warning: the warning severity.
779 % o reason: Define the reason for the warning.
781 % o description: Describe the warning.
784 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
785 const char *description)
787 if (warning_handler != (WarningHandler) NULL)
788 (*warning_handler)(warning,reason,description);
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % S e t E r r o r H a n d l e r %
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 % SetErrorHandler() sets the exception handler to the specified method
803 % and returns the previous exception handler.
805 % The format of the SetErrorHandler method is:
807 % ErrorHandler SetErrorHandler(ErrorHandler handler)
809 % A description of each parameter follows:
811 % o handler: the method to handle errors.
814 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
819 previous_handler=error_handler;
820 error_handler=handler;
821 return(previous_handler);
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 % S e t F a t a l E r r o r H a n d l e r %
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835 % SetFatalErrorHandler() sets the fatal exception handler to the specified
836 % method and returns the previous fatal exception handler.
838 % The format of the SetErrorHandler method is:
840 % ErrorHandler SetErrorHandler(ErrorHandler handler)
842 % A description of each parameter follows:
844 % o handler: the method to handle errors.
847 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
852 previous_handler=fatal_error_handler;
853 fatal_error_handler=handler;
854 return(previous_handler);
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % S e t W a r n i n g H a n d l e r %
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 % SetWarningHandler() sets the warning handler to the specified method
869 % and returns the previous warning handler.
871 % The format of the SetWarningHandler method is:
873 % ErrorHandler SetWarningHandler(ErrorHandler handler)
875 % A description of each parameter follows:
877 % o handler: the method to handle warnings.
880 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
885 previous_handler=warning_handler;
886 warning_handler=handler;
887 return(previous_handler);
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 % T h r o w E x c e p t i o n %
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % ThrowException() throws an exception with the specified severity code,
902 % reason, and optional description.
904 % The format of the ThrowException method is:
906 % MagickBooleanType ThrowException(ExceptionInfo *exception,
907 % const ExceptionType severity,const char *reason,
908 % const char *description)
910 % A description of each parameter follows:
912 % o exception: the exception info.
914 % o severity: the severity of the exception.
916 % o reason: the reason for the exception.
918 % o description: the exception description.
921 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
922 const ExceptionType severity,const char *reason,const char *description)
927 register ExceptionInfo
930 assert(exception != (ExceptionInfo *) NULL);
931 assert(exception->signature == MagickCoreSignature);
932 LockSemaphoreInfo(exception->semaphore);
933 exceptions=(LinkedListInfo *) exception->exceptions;
934 if (GetNumberOfElementsInLinkedList(exceptions) > MaxExceptionList)
936 UnlockSemaphoreInfo(exception->semaphore);
939 p=(ExceptionInfo *) GetLastValueInLinkedList(exceptions);
940 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
941 (LocaleCompare(exception->reason,reason) == 0) &&
942 (LocaleCompare(exception->description,description) == 0))
944 UnlockSemaphoreInfo(exception->semaphore);
947 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
948 if (p == (ExceptionInfo *) NULL)
950 UnlockSemaphoreInfo(exception->semaphore);
951 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
953 (void) memset(p,0,sizeof(*p));
954 p->severity=severity;
955 if (reason != (const char *) NULL)
956 p->reason=ConstantString(reason);
957 if (description != (const char *) NULL)
958 p->description=ConstantString(description);
959 p->signature=MagickCoreSignature;
960 (void) AppendValueToLinkedList(exceptions,p);
961 if (p->severity >= exception->severity)
963 exception->severity=p->severity;
964 exception->reason=p->reason;
965 exception->description=p->description;
967 UnlockSemaphoreInfo(exception->semaphore);
968 if (GetNumberOfElementsInLinkedList(exceptions) == MaxExceptionList)
969 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
970 "TooManyExceptions","(exception processing is suspended)");
975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 % T h r o w M a g i c k E x c e p t i o n %
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 % ThrowMagickException logs an exception as determined by the log
986 % configuration file. If an error occurs, MagickFalse is returned
987 % otherwise MagickTrue.
989 % The format of the ThrowMagickException method is:
991 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
992 % const char *module,const char *function,const size_t line,
993 % const ExceptionType severity,const char *tag,const char *format,...)
995 % A description of each parameter follows:
997 % o exception: the exception info.
999 % o filename: the source module filename.
1001 % o function: the function name.
1003 % o line: the line number of the source module.
1005 % o severity: Specifies the numeric error category.
1007 % o tag: the locale tag.
1009 % o format: the output format.
1013 MagickExport MagickBooleanType ThrowMagickExceptionList(
1014 ExceptionInfo *exception,const char *module,const char *function,
1015 const size_t line,const ExceptionType severity,const char *tag,
1016 const char *format,va_list operands)
1019 message[MagickPathExtent],
1020 path[MagickPathExtent],
1021 reason[MagickPathExtent];
1036 assert(exception != (ExceptionInfo *) NULL);
1037 assert(exception->signature == MagickCoreSignature);
1038 locale=GetLocaleExceptionMessage(severity,tag);
1039 (void) CopyMagickString(reason,locale,MagickPathExtent);
1040 (void) ConcatenateMagickString(reason," ",MagickPathExtent);
1041 length=strlen(reason);
1042 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1043 n=vsnprintf(reason+length,MagickPathExtent-length,format,operands);
1045 n=vsprintf(reason+length,format,operands);
1048 reason[MagickPathExtent-1]='\0';
1049 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
1050 GetPathComponent(module,TailPath,path);
1052 if ((severity >= WarningException) && (severity < ErrorException))
1054 if ((severity >= ErrorException) && (severity < FatalErrorException))
1056 if (severity >= FatalErrorException)
1058 (void) FormatLocaleString(message,MagickPathExtent,"%s @ %s/%s/%s/%.20g",
1059 reason,type,path,function,(double) line);
1060 (void) ThrowException(exception,severity,message,(char *) NULL);
1064 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
1065 const char *module,const char *function,const size_t line,
1066 const ExceptionType severity,const char *tag,const char *format,...)
1074 va_start(operands,format);
1075 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,