MagickCore 7.1.1
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
exception.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
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 %
11% %
12% %
13% MagickCore Exception Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1993 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
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/semaphore.h"
54#include "MagickCore/string_.h"
55#include "MagickCore/utility.h"
56#include "MagickCore/utility-private.h"
57
58/*
59 Define declarations.
60*/
61#define MaxExceptionList 64
62
63/*
64 Forward declarations.
65*/
66#if defined(__cplusplus) || defined(c_plusplus)
67extern "C" {
68#endif
69
70static void
71 DefaultErrorHandler(const ExceptionType,const char *,const char *),
72 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *)
73 magick_attribute((__noreturn__)),
74 DefaultWarningHandler(const ExceptionType,const char *,const char *);
75
76#if defined(__cplusplus) || defined(c_plusplus)
77}
78#endif
79
80/*
81 Global declarations.
82*/
83static ErrorHandler
84 error_handler = DefaultErrorHandler;
85
86static FatalErrorHandler
87 fatal_error_handler = DefaultFatalErrorHandler;
88
89static WarningHandler
90 warning_handler = DefaultWarningHandler;
91
92/*
93 Static declarations.
94*/
95static SemaphoreInfo
96 *exception_semaphore = (SemaphoreInfo *) NULL;
97
98/*
99%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100% %
101% %
102% %
103% A c q u i r e E x c e p t i o n I n f o %
104% %
105% %
106% %
107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108%
109% AcquireExceptionInfo() allocates the ExceptionInfo structure.
110%
111% The format of the AcquireExceptionInfo method is:
112%
113% ExceptionInfo *AcquireExceptionInfo(void)
114%
115*/
116MagickExport ExceptionInfo *AcquireExceptionInfo(void)
117{
119 *exception;
120
121 exception=(ExceptionInfo *) AcquireCriticalMemory(sizeof(*exception));
122 InitializeExceptionInfo(exception);
123 exception->relinquish=MagickTrue;
124 return(exception);
125}
126
127/*
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129% %
130% %
131% %
132% C l e a r M a g i c k E x c e p t i o n %
133% %
134% %
135% %
136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137%
138% ClearMagickException() clears any exception that may not have been caught
139% yet.
140%
141% The format of the ClearMagickException method is:
142%
143% ClearMagickException(ExceptionInfo *exception)
144%
145% A description of each parameter follows:
146%
147% o exception: the exception info.
148%
149*/
150
151static void *DestroyExceptionElement(void *exception)
152{
154 *p;
155
156 p=(ExceptionInfo *) exception;
157 if (p->reason != (char *) NULL)
158 p->reason=DestroyString(p->reason);
159 if (p->description != (char *) NULL)
160 p->description=DestroyString(p->description);
161 p=(ExceptionInfo *) RelinquishMagickMemory(p);
162 return((void *) NULL);
163}
164
165MagickExport void ClearMagickException(ExceptionInfo *exception)
166{
167 assert(exception != (ExceptionInfo *) NULL);
168 assert(exception->signature == MagickCoreSignature);
169 if (exception->exceptions == (void *) NULL)
170 return;
171 LockSemaphoreInfo(exception->semaphore);
172 ClearLinkedList((LinkedListInfo *) exception->exceptions,
173 DestroyExceptionElement);
174 exception->severity=UndefinedException;
175 exception->reason=(char *) NULL;
176 exception->description=(char *) NULL;
177 UnlockSemaphoreInfo(exception->semaphore);
178 errno=0;
179}
180
181/*
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183% %
184% %
185% %
186% C a t c h E x c e p t i o n %
187% %
188% %
189% %
190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191%
192% CatchException() returns if no exceptions is found otherwise it reports
193% the exception as a warning, error, or fatal depending on the severity.
194%
195% The format of the CatchException method is:
196%
197% CatchException(ExceptionInfo *exception)
198%
199% A description of each parameter follows:
200%
201% o exception: the exception info.
202%
203*/
204MagickExport void CatchException(ExceptionInfo *exception)
205{
207 *exceptions;
208
209 const ExceptionInfo
210 *p;
211
212 assert(exception != (ExceptionInfo *) NULL);
213 assert(exception->signature == MagickCoreSignature);
214 if (exception->exceptions == (void *) NULL)
215 return;
216 LockSemaphoreInfo(exception->semaphore);
217 exceptions=(LinkedListInfo *) exception->exceptions;
218 ResetLinkedListIterator(exceptions);
219 p=(const ExceptionInfo *) GetNextValueInLinkedList(exceptions);
220 while (p != (const ExceptionInfo *) NULL)
221 {
222 if ((p->severity >= WarningException) && (p->severity < ErrorException))
223 MagickWarning(p->severity,p->reason,p->description);
224 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
225 MagickError(p->severity,p->reason,p->description);
226 if (p->severity >= FatalErrorException)
227 MagickFatalError(p->severity,p->reason,p->description);
228 p=(const ExceptionInfo *) GetNextValueInLinkedList(exceptions);
229 }
230 UnlockSemaphoreInfo(exception->semaphore);
231 ClearMagickException(exception);
232}
233
234/*
235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236% %
237% %
238% %
239% C l o n e E x c e p t i o n I n f o %
240% %
241% %
242% %
243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244%
245% CloneExceptionInfo() clones the ExceptionInfo structure.
246%
247% The format of the CloneExceptionInfo method is:
248%
249% ExceptionInfo *CloneException(ExceptionInfo *exception)
250%
251% A description of each parameter follows:
252%
253% o exception: the exception info.
254%
255*/
256MagickExport ExceptionInfo *CloneExceptionInfo(ExceptionInfo *exception)
257{
259 *clone_exception;
260
261 clone_exception=(ExceptionInfo *) AcquireCriticalMemory(sizeof(*exception));
262 InitializeExceptionInfo(clone_exception);
263 InheritException(clone_exception,exception);
264 clone_exception->relinquish=MagickTrue;
265 return(clone_exception);
266}
267
268/*
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270% %
271% %
272% %
273+ D e f a u l t E r r o r H a n d l e r %
274% %
275% %
276% %
277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278%
279% DefaultErrorHandler() displays an error reason.
280%
281% The format of the DefaultErrorHandler method is:
282%
283% void MagickError(const ExceptionType severity,const char *reason,
284% const char *description)
285%
286% A description of each parameter follows:
287%
288% o severity: Specifies the numeric error category.
289%
290% o reason: Specifies the reason to display before terminating the
291% program.
292%
293% o description: Specifies any description to the reason.
294%
295*/
296static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
297 const char *reason,const char *description)
298{
299 magick_unreferenced(severity);
300
301 if (reason == (char *) NULL)
302 return;
303 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
304 if (description != (char *) NULL)
305 (void) FormatLocaleFile(stderr," (%s)",description);
306 (void) FormatLocaleFile(stderr,".\n");
307 (void) fflush(stderr);
308}
309
310/*
311%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312% %
313% %
314% %
315+ D e f a u l t F a t a l E r r o r H a n d l e r %
316% %
317% %
318% %
319%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320%
321% DefaultFatalErrorHandler() displays an error reason and then terminates the
322% program.
323%
324% The format of the DefaultFatalErrorHandler method is:
325%
326% void MagickFatalError(const ExceptionType severity,const char *reason,
327% const char *description)
328%
329% A description of each parameter follows:
330%
331% o severity: Specifies the numeric error category.
332%
333% o reason: Specifies the reason to display before terminating the program.
334%
335% o description: Specifies any description to the reason.
336%
337*/
338static void DefaultFatalErrorHandler(const ExceptionType severity,
339 const char *reason,const char *description)
340{
341 (void) FormatLocaleFile(stderr,"%s:",GetClientName());
342 if (reason != (char *) NULL)
343 (void) FormatLocaleFile(stderr," %s",reason);
344 if (description != (char *) NULL)
345 (void) FormatLocaleFile(stderr," (%s)",description);
346 (void) FormatLocaleFile(stderr,".\n");
347 (void) fflush(stderr);
348 MagickCoreTerminus();
349 exit((int) (severity-FatalErrorException)+1);
350}
351
352/*
353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354% %
355% %
356% %
357+ D e f a u l t W a r n i n g H a n d l e r %
358% %
359% %
360% %
361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362%
363% DefaultWarningHandler() displays a warning reason.
364%
365% The format of the DefaultWarningHandler method is:
366%
367% void DefaultWarningHandler(const ExceptionType severity,
368% const char *reason,const char *description)
369%
370% A description of each parameter follows:
371%
372% o severity: Specifies the numeric warning category.
373%
374% o reason: Specifies the reason to display before terminating the
375% program.
376%
377% o description: Specifies any description to the reason.
378%
379*/
380static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
381 const char *reason,const char *description)
382{
383 magick_unreferenced(severity);
384
385 if (reason == (char *) NULL)
386 return;
387 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
388 if (description != (char *) NULL)
389 (void) FormatLocaleFile(stderr," (%s)",description);
390 (void) FormatLocaleFile(stderr,".\n");
391 (void) fflush(stderr);
392}
393
394/*
395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396% %
397% %
398% %
399% D e s t r o y E x c e p t i o n I n f o %
400% %
401% %
402% %
403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404%
405% DestroyExceptionInfo() deallocates memory associated with an exception.
406%
407% The format of the DestroyExceptionInfo method is:
408%
409% ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
410%
411% A description of each parameter follows:
412%
413% o exception: the exception info.
414%
415*/
416MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
417{
418 MagickBooleanType
419 relinquish;
420
421 assert(exception != (ExceptionInfo *) NULL);
422 assert(exception->signature == MagickCoreSignature);
423 if (exception->semaphore == (SemaphoreInfo *) NULL)
424 ActivateSemaphoreInfo(&exception->semaphore);
425 LockSemaphoreInfo(exception->semaphore);
426 exception->severity=UndefinedException;
427 if (exception->relinquish != MagickFalse)
428 {
429 exception->signature=(~MagickCoreSignature);
430 if (exception->exceptions != (void *) NULL)
431 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
432 exception->exceptions,DestroyExceptionElement);
433 }
434 else
435 if (exception->exceptions != (void *) NULL)
436 ClearLinkedList((LinkedListInfo *) exception->exceptions,
437 DestroyExceptionElement);
438 relinquish=exception->relinquish;
439 UnlockSemaphoreInfo(exception->semaphore);
440 if (relinquish != MagickFalse)
441 {
442 RelinquishSemaphoreInfo(&exception->semaphore);
443 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
444 }
445 return(exception);
446}
447
448/*
449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450% %
451% %
452% %
453+ E x e c e p t i o n C o m p o n e n t G e n e s i s %
454% %
455% %
456% %
457%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458%
459% ExceptionComponentGenesis() instantiates the exception component.
460%
461% The format of the ExceptionComponentGenesis method is:
462%
463% MagickBooleanType ExceptionComponentGenesis(void)
464%
465*/
466MagickPrivate MagickBooleanType ExceptionComponentGenesis(void)
467{
468 if (exception_semaphore == (SemaphoreInfo *) NULL)
469 exception_semaphore=AcquireSemaphoreInfo();
470 return(MagickTrue);
471}
472
473/*
474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475% %
476% %
477% %
478+ E x c e p t i o n C o m p o n e n t T e r m i n u s %
479% %
480% %
481% %
482%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483%
484% ExceptionComponentTerminus() destroys the exception component.
485%
486% The format of the ExceptionComponentTerminus method is:
487%
488% void ExceptionComponentTerminus(void)
489%
490*/
491MagickPrivate void ExceptionComponentTerminus(void)
492{
493 if (exception_semaphore == (SemaphoreInfo *) NULL)
494 ActivateSemaphoreInfo(&exception_semaphore);
495 LockSemaphoreInfo(exception_semaphore);
496 UnlockSemaphoreInfo(exception_semaphore);
497 RelinquishSemaphoreInfo(&exception_semaphore);
498}
499
500/*
501%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502% %
503% %
504% %
505% G e t E x c e p t i o n M e s s a g e %
506% %
507% %
508% %
509%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
510%
511% GetExceptionMessage() returns the error message defined by the specified
512% error code.
513%
514% The format of the GetExceptionMessage method is:
515%
516% char *GetExceptionMessage(const int error)
517%
518% A description of each parameter follows:
519%
520% o error: the error code.
521%
522*/
523MagickExport char *GetExceptionMessage(const int error)
524{
525 char
526 exception[MagickPathExtent];
527
528 *exception='\0';
529#if defined(MAGICKCORE_HAVE_STRERROR_R)
530#if !defined(MAGICKCORE_STRERROR_R_CHAR_P)
531 (void) strerror_r(error,exception,sizeof(exception));
532#else
533 (void) CopyMagickString(exception,strerror_r(error,exception,
534 sizeof(exception)),sizeof(exception));
535#endif
536#else
537 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
538#endif
539 return(ConstantString(exception));
540}
541
542/*
543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544% %
545% %
546% %
547% 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 %
548% %
549% %
550% %
551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552%
553% GetLocaleExceptionMessage() converts a enumerated exception severity and tag
554% to a message in the current locale.
555%
556% The format of the GetLocaleExceptionMessage method is:
557%
558% const char *GetLocaleExceptionMessage(const ExceptionType severity,
559% const char *tag)
560%
561% A description of each parameter follows:
562%
563% o severity: the severity of the exception.
564%
565% o tag: the message tag.
566%
567*/
568
569static const char *ExceptionSeverityToTag(const ExceptionType severity)
570{
571 switch (severity)
572 {
573 case ResourceLimitWarning: return("Resource/Limit/Warning/");
574 case TypeWarning: return("Type/Warning/");
575 case OptionWarning: return("Option/Warning/");
576 case DelegateWarning: return("Delegate/Warning/");
577 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
578 case CorruptImageWarning: return("Corrupt/Image/Warning/");
579 case FileOpenWarning: return("File/Open/Warning/");
580 case BlobWarning: return("Blob/Warning/");
581 case StreamWarning: return("Stream/Warning/");
582 case CacheWarning: return("Cache/Warning/");
583 case CoderWarning: return("Coder/Warning/");
584 case FilterWarning: return("Filter/Warning/");
585 case ModuleWarning: return("Module/Warning/");
586 case DrawWarning: return("Draw/Warning/");
587 case ImageWarning: return("Image/Warning/");
588 case WandWarning: return("Wand/Warning/");
589 case XServerWarning: return("XServer/Warning/");
590 case MonitorWarning: return("Monitor/Warning/");
591 case RegistryWarning: return("Registry/Warning/");
592 case ConfigureWarning: return("Configure/Warning/");
593 case PolicyWarning: return("Policy/Warning/");
594 case ResourceLimitError: return("Resource/Limit/Error/");
595 case TypeError: return("Type/Error/");
596 case OptionError: return("Option/Error/");
597 case DelegateError: return("Delegate/Error/");
598 case MissingDelegateError: return("Missing/Delegate/Error/");
599 case CorruptImageError: return("Corrupt/Image/Error/");
600 case FileOpenError: return("File/Open/Error/");
601 case BlobError: return("Blob/Error/");
602 case StreamError: return("Stream/Error/");
603 case CacheError: return("Cache/Error/");
604 case CoderError: return("Coder/Error/");
605 case FilterError: return("Filter/Error/");
606 case ModuleError: return("Module/Error/");
607 case DrawError: return("Draw/Error/");
608 case ImageError: return("Image/Error/");
609 case WandError: return("Wand/Error/");
610 case XServerError: return("XServer/Error/");
611 case MonitorError: return("Monitor/Error/");
612 case RegistryError: return("Registry/Error/");
613 case ConfigureError: return("Configure/Error/");
614 case PolicyError: return("Policy/Error/");
615 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
616 case TypeFatalError: return("Type/FatalError/");
617 case OptionFatalError: return("Option/FatalError/");
618 case DelegateFatalError: return("Delegate/FatalError/");
619 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
620 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
621 case FileOpenFatalError: return("File/Open/FatalError/");
622 case BlobFatalError: return("Blob/FatalError/");
623 case StreamFatalError: return("Stream/FatalError/");
624 case CacheFatalError: return("Cache/FatalError/");
625 case CoderFatalError: return("Coder/FatalError/");
626 case FilterFatalError: return("Filter/FatalError/");
627 case ModuleFatalError: return("Module/FatalError/");
628 case DrawFatalError: return("Draw/FatalError/");
629 case ImageFatalError: return("Image/FatalError/");
630 case WandFatalError: return("Wand/FatalError/");
631 case XServerFatalError: return("XServer/FatalError/");
632 case MonitorFatalError: return("Monitor/FatalError/");
633 case RegistryFatalError: return("Registry/FatalError/");
634 case ConfigureFatalError: return("Configure/FatalError/");
635 case PolicyFatalError: return("Policy/FatalError/");
636 default: break;
637 }
638 return("");
639}
640
641MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
642 const char *tag)
643{
644 char
645 message[MagickPathExtent];
646
647 const char
648 *locale_message;
649
650 assert(tag != (const char *) NULL);
651 (void) FormatLocaleString(message,MagickPathExtent,"Exception/%s%s",
652 ExceptionSeverityToTag(severity),tag);
653 locale_message=GetLocaleMessage(message);
654 if (locale_message == (const char *) NULL)
655 return(tag);
656 if (locale_message == message)
657 return(tag);
658 return(locale_message);
659}
660
661/*
662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663% %
664% %
665% %
666% I n h e r i t E x c e p t i o n %
667% %
668% %
669% %
670%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671%
672% InheritException() inherits an exception from a related exception.
673%
674% The format of the InheritException method is:
675%
676% InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
677%
678% A description of each parameter follows:
679%
680% o exception: the exception info.
681%
682% o relative: the related exception info.
683%
684*/
685MagickExport void InheritException(ExceptionInfo *exception,
686 const ExceptionInfo *relative)
687{
688 const ExceptionInfo
689 *p;
690
691 assert(exception != (ExceptionInfo *) NULL);
692 assert(exception->signature == MagickCoreSignature);
693 assert(relative != (ExceptionInfo *) NULL);
694 assert(relative->signature == MagickCoreSignature);
695 assert(exception != relative);
696 if (relative->exceptions == (void *) NULL)
697 return;
698 LockSemaphoreInfo(relative->semaphore);
699 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
700 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
701 relative->exceptions);
702 while (p != (const ExceptionInfo *) NULL)
703 {
704 (void) ThrowException(exception,p->severity,p->reason,p->description);
705 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
706 relative->exceptions);
707 }
708 UnlockSemaphoreInfo(relative->semaphore);
709}
710
711/*
712%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713% %
714% %
715% %
716% I n i t i a l i z e t E x c e p t i o n I n f o %
717% %
718% %
719% %
720%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721%
722% InitializeExceptionInfo() initializes an exception to default values.
723%
724% The format of the InitializeExceptionInfo method is:
725%
726% InitializeExceptionInfo(ExceptionInfo *exception)
727%
728% A description of each parameter follows:
729%
730% o exception: the exception info.
731%
732*/
733MagickPrivate void InitializeExceptionInfo(ExceptionInfo *exception)
734{
735 assert(exception != (ExceptionInfo *) NULL);
736 (void) memset(exception,0,sizeof(*exception));
737 exception->severity=UndefinedException;
738 exception->exceptions=(void *) NewLinkedList(0);
739 exception->semaphore=AcquireSemaphoreInfo();
740 exception->signature=MagickCoreSignature;
741}
742
743/*
744%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
745% %
746% %
747% %
748% M a g i c k E r r o r %
749% %
750% %
751% %
752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753%
754% MagickError() calls the exception handler methods with an error reason.
755%
756% The format of the MagickError method is:
757%
758% void MagickError(const ExceptionType error,const char *reason,
759% const char *description)
760%
761% A description of each parameter follows:
762%
763% o exception: Specifies the numeric error category.
764%
765% o reason: Specifies the reason to display before terminating the
766% program.
767%
768% o description: Specifies any description to the reason.
769%
770*/
771MagickExport void MagickError(const ExceptionType error,const char *reason,
772 const char *description)
773{
774 if (error_handler != (ErrorHandler) NULL)
775 (*error_handler)(error,reason,description);
776}
777
778/*
779%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780% %
781% %
782% %
783% M a g i c k F a t al E r r o r %
784% %
785% %
786% %
787%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788%
789% MagickFatalError() calls the fatal exception handler methods with an error
790% reason.
791%
792% The format of the MagickError method is:
793%
794% void MagickFatalError(const ExceptionType error,const char *reason,
795% const char *description)
796%
797% A description of each parameter follows:
798%
799% o exception: Specifies the numeric error category.
800%
801% o reason: Specifies the reason to display before terminating the
802% program.
803%
804% o description: Specifies any description to the reason.
805%
806*/
807MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
808 const char *description)
809{
810 if (fatal_error_handler != (FatalErrorHandler) NULL)
811 (*fatal_error_handler)(error,reason,description);
812 MagickCoreTerminus();
813 exit(1);
814}
815
816/*
817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818% %
819% %
820% %
821% M a g i c k W a r n i n g %
822% %
823% %
824% %
825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826%
827% MagickWarning() calls the warning handler methods with a warning reason.
828%
829% The format of the MagickWarning method is:
830%
831% void MagickWarning(const ExceptionType warning,const char *reason,
832% const char *description)
833%
834% A description of each parameter follows:
835%
836% o warning: the warning severity.
837%
838% o reason: Define the reason for the warning.
839%
840% o description: Describe the warning.
841%
842*/
843MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
844 const char *description)
845{
846 if (warning_handler != (WarningHandler) NULL)
847 (*warning_handler)(warning,reason,description);
848}
849
850/*
851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852% %
853% %
854% %
855% S e t E r r o r H a n d l e r %
856% %
857% %
858% %
859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860%
861% SetErrorHandler() sets the exception handler to the specified method
862% and returns the previous exception handler.
863%
864% The format of the SetErrorHandler method is:
865%
866% ErrorHandler SetErrorHandler(ErrorHandler handler)
867%
868% A description of each parameter follows:
869%
870% o handler: the method to handle errors.
871%
872*/
873MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
874{
875 ErrorHandler
876 previous_handler;
877
878 if (exception_semaphore == (SemaphoreInfo *) NULL)
879 ActivateSemaphoreInfo(&exception_semaphore);
880 LockSemaphoreInfo(exception_semaphore);
881 previous_handler=error_handler;
882 error_handler=handler;
883 UnlockSemaphoreInfo(exception_semaphore);
884 return(previous_handler);
885}
886
887/*
888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889% %
890% %
891% %
892% S e t F a t a l E r r o r H a n d l e r %
893% %
894% %
895% %
896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897%
898% SetFatalErrorHandler() sets the fatal exception handler to the specified
899% method and returns the previous fatal exception handler.
900%
901% The format of the SetErrorHandler method is:
902%
903% ErrorHandler SetErrorHandler(ErrorHandler handler)
904%
905% A description of each parameter follows:
906%
907% o handler: the method to handle errors.
908%
909*/
910MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
911{
912 FatalErrorHandler
913 previous_handler;
914
915 if (exception_semaphore == (SemaphoreInfo *) NULL)
916 ActivateSemaphoreInfo(&exception_semaphore);
917 LockSemaphoreInfo(exception_semaphore);
918 previous_handler=fatal_error_handler;
919 fatal_error_handler=handler;
920 UnlockSemaphoreInfo(exception_semaphore);
921 return(previous_handler);
922}
923
924/*
925%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
926% %
927% %
928% %
929% S e t W a r n i n g H a n d l e r %
930% %
931% %
932% %
933%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934%
935% SetWarningHandler() sets the warning handler to the specified method
936% and returns the previous warning handler.
937%
938% The format of the SetWarningHandler method is:
939%
940% ErrorHandler SetWarningHandler(ErrorHandler handler)
941%
942% A description of each parameter follows:
943%
944% o handler: the method to handle warnings.
945%
946*/
947MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
948{
949 WarningHandler
950 previous_handler;
951
952 if (exception_semaphore == (SemaphoreInfo *) NULL)
953 ActivateSemaphoreInfo(&exception_semaphore);
954 LockSemaphoreInfo(exception_semaphore);
955 previous_handler=warning_handler;
956 warning_handler=handler;
957 UnlockSemaphoreInfo(exception_semaphore);
958 return(previous_handler);
959}
960
961/*
962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963% %
964% %
965% %
966% T h r o w E x c e p t i o n %
967% %
968% %
969% %
970%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971%
972% ThrowException() throws an exception with the specified severity code,
973% reason, and optional description.
974%
975% The format of the ThrowException method is:
976%
977% MagickBooleanType ThrowException(ExceptionInfo *exception,
978% const ExceptionType severity,const char *reason,
979% const char *description)
980%
981% A description of each parameter follows:
982%
983% o exception: the exception info.
984%
985% o severity: the severity of the exception.
986%
987% o reason: the reason for the exception.
988%
989% o description: the exception description.
990%
991*/
992MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
993 const ExceptionType severity,const char *reason,const char *description)
994{
996 *exceptions;
997
999 *p;
1000
1001 assert(exception != (ExceptionInfo *) NULL);
1002 assert(exception->signature == MagickCoreSignature);
1003 LockSemaphoreInfo(exception->semaphore);
1004 exceptions=(LinkedListInfo *) exception->exceptions;
1005 if (GetNumberOfElementsInLinkedList(exceptions) > MaxExceptionList)
1006 {
1007 if (severity < ErrorException)
1008 {
1009 UnlockSemaphoreInfo(exception->semaphore);
1010 return(MagickTrue);
1011 }
1012 p=(ExceptionInfo *) GetLastValueInLinkedList(exceptions);
1013 if (p->severity >= ErrorException)
1014 {
1015 UnlockSemaphoreInfo(exception->semaphore);
1016 return(MagickTrue);
1017 }
1018 }
1019 p=(ExceptionInfo *) GetLastValueInLinkedList(exceptions);
1020 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
1021 (LocaleCompare(exception->reason,reason) == 0) &&
1022 (LocaleCompare(exception->description,description) == 0))
1023 {
1024 UnlockSemaphoreInfo(exception->semaphore);
1025 return(MagickTrue);
1026 }
1027 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
1028 if (p == (ExceptionInfo *) NULL)
1029 {
1030 UnlockSemaphoreInfo(exception->semaphore);
1031 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1032 }
1033 (void) memset(p,0,sizeof(*p));
1034 p->severity=severity;
1035 if (reason != (const char *) NULL)
1036 p->reason=ConstantString(reason);
1037 if (description != (const char *) NULL)
1038 p->description=ConstantString(description);
1039 p->signature=MagickCoreSignature;
1040 (void) AppendValueToLinkedList(exceptions,p);
1041 if (p->severity > exception->severity)
1042 {
1043 exception->severity=p->severity;
1044 exception->reason=p->reason;
1045 exception->description=p->description;
1046 }
1047 UnlockSemaphoreInfo(exception->semaphore);
1048 if (GetNumberOfElementsInLinkedList(exceptions) == MaxExceptionList)
1049 (void) ThrowMagickException(exception,GetMagickModule(),
1050 ResourceLimitWarning,"TooManyExceptions",
1051 "(exception processing is suspended)");
1052 return(MagickTrue);
1053}
1054
1055/*
1056%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1057% %
1058% %
1059% %
1060% T h r o w M a g i c k E x c e p t i o n %
1061% %
1062% %
1063% %
1064%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065%
1066% ThrowMagickException logs an exception as determined by the log
1067% configuration file. If an error occurs, MagickFalse is returned
1068% otherwise MagickTrue.
1069%
1070% The format of the ThrowMagickException method is:
1071%
1072% MagickBooleanType ThrowFileException(ExceptionInfo *exception,
1073% const char *module,const char *function,const size_t line,
1074% const ExceptionType severity,const char *tag,const char *format,...)
1075%
1076% A description of each parameter follows:
1077%
1078% o exception: the exception info.
1079%
1080% o filename: the source module filename.
1081%
1082% o function: the function name.
1083%
1084% o line: the line number of the source module.
1085%
1086% o severity: Specifies the numeric error category.
1087%
1088% o tag: the locale tag.
1089%
1090% o format: the output format.
1091%
1092*/
1093
1094MagickExport MagickBooleanType ThrowMagickExceptionList(
1095 ExceptionInfo *exception,const char *module,const char *function,
1096 const size_t line,const ExceptionType severity,const char *tag,
1097 const char *format,va_list operands)
1098{
1099 char
1100 message[MagickPathExtent],
1101 path[MagickPathExtent],
1102 reason[MagickPathExtent];
1103
1104 const char
1105 *locale,
1106 *type;
1107
1108 int
1109 n;
1110
1111 MagickBooleanType
1112 status;
1113
1114 size_t
1115 length;
1116
1117 assert(exception != (ExceptionInfo *) NULL);
1118 assert(exception->signature == MagickCoreSignature);
1119 locale=GetLocaleExceptionMessage(severity,tag);
1120 (void) CopyMagickString(reason,locale,MagickPathExtent);
1121 (void) ConcatenateMagickString(reason," ",MagickPathExtent);
1122 length=strlen(reason);
1123#if defined(MAGICKCORE_HAVE_VSNPRINTF)
1124 n=vsnprintf(reason+length,MagickPathExtent-length,format,operands);
1125#else
1126 n=vsprintf(reason+length,format,operands);
1127#endif
1128 if (n < 0)
1129 reason[MagickPathExtent-1]='\0';
1130 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
1131 GetPathComponent(module,TailPath,path);
1132 type="undefined";
1133 if ((severity >= WarningException) && (severity < ErrorException))
1134 type="warning";
1135 if ((severity >= ErrorException) && (severity < FatalErrorException))
1136 type="error";
1137 if (severity >= FatalErrorException)
1138 type="fatal";
1139 (void) FormatLocaleString(message,MagickPathExtent,"%s @ %s/%s/%s/%.20g",
1140 reason,type,path,function,(double) line);
1141 (void) ThrowException(exception,severity,message,(char *) NULL);
1142 return(status);
1143}
1144
1145MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
1146 const char *module,const char *function,const size_t line,
1147 const ExceptionType severity,const char *tag,const char *format,...)
1148{
1149 MagickBooleanType
1150 status;
1151
1152 va_list
1153 operands;
1154
1155 va_start(operands,format);
1156 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
1157 format,operands);
1158 va_end(operands);
1159 return(status);
1160}