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