]> granicus.if.org Git - imagemagick/blob - MagickCore/exception.c
simplify boolean expression, found by PVS analyzer (#1318)
[imagemagick] / MagickCore / 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-2018 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 \f
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/string_.h"
54 #include "MagickCore/utility.h"
55 #include "MagickCore/utility-private.h"
56 \f
57 /*
58   Define declarations.
59 */
60 #define MaxExceptionList  64
61 \f
62 /*
63   Forward declarations.
64 */
65 #if defined(__cplusplus) || defined(c_plusplus)
66 extern "C" {
67 #endif
68
69 static void
70   DefaultErrorHandler(const ExceptionType,const char *,const char *),
71   DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
72   DefaultWarningHandler(const ExceptionType,const char *,const char *);
73
74 #if defined(__cplusplus) || defined(c_plusplus)
75 }
76 #endif
77 \f
78 /*
79   Global declarations.
80 */
81 static ErrorHandler
82   error_handler = DefaultErrorHandler;
83
84 static FatalErrorHandler
85   fatal_error_handler = DefaultFatalErrorHandler;
86
87 static WarningHandler
88   warning_handler = DefaultWarningHandler;
89 \f
90 /*
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 %                                                                             %
93 %                                                                             %
94 %                                                                             %
95 %   A c q u i r e E x c e p t i o n I n f o                                   %
96 %                                                                             %
97 %                                                                             %
98 %                                                                             %
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 %
101 %  AcquireExceptionInfo() allocates the ExceptionInfo structure.
102 %
103 %  The format of the AcquireExceptionInfo method is:
104 %
105 %      ExceptionInfo *AcquireExceptionInfo(void)
106 %
107 */
108 MagickExport ExceptionInfo *AcquireExceptionInfo(void)
109 {
110   ExceptionInfo
111     *exception;
112
113   exception=(ExceptionInfo *) AcquireCriticalMemory(sizeof(*exception));
114   InitializeExceptionInfo(exception);
115   exception->relinquish=MagickTrue;
116   return(exception);
117 }
118 \f
119 /*l
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 %                                                                             %
122 %                                                                             %
123 %                                                                             %
124 %   C l e a r M a g i c k E x c e p t i o n                                   %
125 %                                                                             %
126 %                                                                             %
127 %                                                                             %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 %
130 %  ClearMagickException() clears any exception that may not have been caught
131 %  yet.
132 %
133 %  The format of the ClearMagickException method is:
134 %
135 %      ClearMagickException(ExceptionInfo *exception)
136 %
137 %  A description of each parameter follows:
138 %
139 %    o exception: the exception info.
140 %
141 */
142
143 static void *DestroyExceptionElement(void *exception)
144 {
145   register ExceptionInfo
146     *p;
147
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);
155 }
156
157 MagickExport void ClearMagickException(ExceptionInfo *exception)
158 {
159   assert(exception != (ExceptionInfo *) NULL);
160   assert(exception->signature == MagickCoreSignature);
161   if (exception->exceptions == (void *) NULL)
162     return;
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);
170   errno=0;
171 }
172 \f
173 /*
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 %                                                                             %
176 %                                                                             %
177 %                                                                             %
178 %   C a t c h E x c e p t i o n                                               %
179 %                                                                             %
180 %                                                                             %
181 %                                                                             %
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 %
184 %  CatchException() returns if no exceptions is found otherwise it reports
185 %  the exception as a warning, error, or fatal depending on the severity.
186 %
187 %  The format of the CatchException method is:
188 %
189 %      CatchException(ExceptionInfo *exception)
190 %
191 %  A description of each parameter follows:
192 %
193 %    o exception: the exception info.
194 %
195 */
196 MagickExport void CatchException(ExceptionInfo *exception)
197 {
198   LinkedListInfo
199     *exceptions;
200
201   register const ExceptionInfo
202     *p;
203
204   ssize_t
205     i;
206
207   assert(exception != (ExceptionInfo *) NULL);
208   assert(exception->signature == MagickCoreSignature);
209   if (exception->exceptions  == (void *) NULL)
210     return;
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++)
216   {
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);
224   }
225   UnlockSemaphoreInfo(exception->semaphore);
226   ClearMagickException(exception);
227 }
228 \f
229 /*
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 %                                                                             %
232 %                                                                             %
233 %                                                                             %
234 %   C l o n e E x c e p t i o n I n f o                                       %
235 %                                                                             %
236 %                                                                             %
237 %                                                                             %
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 %
240 %  CloneExceptionInfo() clones the ExceptionInfo structure.
241 %
242 %  The format of the CloneExceptionInfo method is:
243 %
244 %      ExceptionInfo *CloneException(ExceptionInfo *exception)
245 %
246 %  A description of each parameter follows:
247 %
248 %    o exception: the exception info.
249 %
250 */
251 MagickExport ExceptionInfo *CloneExceptionInfo(ExceptionInfo *exception)
252 {
253   ExceptionInfo
254     *clone_exception;
255
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);
261 }
262 \f
263 /*
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 %                                                                             %
266 %                                                                             %
267 %                                                                             %
268 +   D e f a u l t E r r o r H a n d l e r                                     %
269 %                                                                             %
270 %                                                                             %
271 %                                                                             %
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 %
274 %  DefaultErrorHandler() displays an error reason.
275 %
276 %  The format of the DefaultErrorHandler method is:
277 %
278 %      void MagickError(const ExceptionType severity,const char *reason,
279 %        const char *description)
280 %
281 %  A description of each parameter follows:
282 %
283 %    o severity: Specifies the numeric error category.
284 %
285 %    o reason: Specifies the reason to display before terminating the
286 %      program.
287 %
288 %    o description: Specifies any description to the reason.
289 %
290 */
291 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
292   const char *reason,const char *description)
293 {
294   magick_unreferenced(severity);
295
296   if (reason == (char *) NULL)
297     return;
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);
303 }
304 \f
305 /*
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 %                                                                             %
308 %                                                                             %
309 %                                                                             %
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                           %
311 %                                                                             %
312 %                                                                             %
313 %                                                                             %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 %
316 %  DefaultFatalErrorHandler() displays an error reason and then terminates the
317 %  program.
318 %
319 %  The format of the DefaultFatalErrorHandler method is:
320 %
321 %      void MagickFatalError(const ExceptionType severity,const char *reason,
322 %        const char *description)
323 %
324 %  A description of each parameter follows:
325 %
326 %    o severity: Specifies the numeric error category.
327 %
328 %    o reason: Specifies the reason to display before terminating the program.
329 %
330 %    o description: Specifies any description to the reason.
331 %
332 */
333 static void DefaultFatalErrorHandler(const ExceptionType severity,
334   const char *reason,const char *description)
335 {
336   if (reason == (char *) NULL)
337     return;
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);
345 }
346 \f
347 /*
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 %                                                                             %
350 %                                                                             %
351 %                                                                             %
352 +   D e f a u l t W a r n i n g H a n d l e r                                 %
353 %                                                                             %
354 %                                                                             %
355 %                                                                             %
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 %
358 %  DefaultWarningHandler() displays a warning reason.
359 %
360 %  The format of the DefaultWarningHandler method is:
361 %
362 %      void DefaultWarningHandler(const ExceptionType severity,
363 %        const char *reason,const char *description)
364 %
365 %  A description of each parameter follows:
366 %
367 %    o severity: Specifies the numeric warning category.
368 %
369 %    o reason: Specifies the reason to display before terminating the
370 %      program.
371 %
372 %    o description: Specifies any description to the reason.
373 %
374 */
375 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
376   const char *reason,const char *description)
377 {
378   magick_unreferenced(severity);
379
380   if (reason == (char *) NULL)
381     return;
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);
387 }
388 \f
389 /*
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 %                                                                             %
392 %                                                                             %
393 %                                                                             %
394 %   D e s t r o y E x c e p t i o n I n f o                                   %
395 %                                                                             %
396 %                                                                             %
397 %                                                                             %
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 %
400 %  DestroyExceptionInfo() deallocates memory associated with an exception.
401 %
402 %  The format of the DestroyExceptionInfo method is:
403 %
404 %      ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
405 %
406 %  A description of each parameter follows:
407 %
408 %    o exception: the exception info.
409 %
410 */
411 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
412 {
413   MagickBooleanType
414     relinquish;
415
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)
423     {
424       exception->signature=(~MagickCoreSignature);
425       if (exception->exceptions != (void *) NULL)
426         exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
427           exception->exceptions,DestroyExceptionElement);
428     }
429   else
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)
436     {
437       RelinquishSemaphoreInfo(&exception->semaphore);
438       exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
439     }
440   return(exception);
441 }
442 \f
443 /*
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445 %                                                                             %
446 %                                                                             %
447 %                                                                             %
448 %   G e t E x c e p t i o n M e s s a g e                                     %
449 %                                                                             %
450 %                                                                             %
451 %                                                                             %
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 %
454 %  GetExceptionMessage() returns the error message defined by the specified
455 %  error code.
456 %
457 %  The format of the GetExceptionMessage method is:
458 %
459 %      char *GetExceptionMessage(const int error)
460 %
461 %  A description of each parameter follows:
462 %
463 %    o error: the error code.
464 %
465 */
466 MagickExport char *GetExceptionMessage(const int error)
467 {
468   char
469     exception[MagickPathExtent];
470
471   *exception='\0';
472 #if defined(MAGICKCORE_HAVE_STRERROR_R)
473 #if !defined(MAGICKCORE_STRERROR_R_CHAR_P)
474   (void) strerror_r(error,exception,sizeof(exception));
475 #else
476   (void) CopyMagickString(exception,strerror_r(error,exception,
477     sizeof(exception)),sizeof(exception));
478 #endif
479 #else
480   (void) CopyMagickString(exception,strerror(error),sizeof(exception));
481 #endif
482   return(ConstantString(exception));
483 }
484 \f
485 /*
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 %                                                                             %
488 %                                                                             %
489 %                                                                             %
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                         %
491 %                                                                             %
492 %                                                                             %
493 %                                                                             %
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 %
496 %  GetLocaleExceptionMessage() converts a enumerated exception severity and tag
497 %  to a message in the current locale.
498 %
499 %  The format of the GetLocaleExceptionMessage method is:
500 %
501 %      const char *GetLocaleExceptionMessage(const ExceptionType severity,
502 %        const char *tag)
503 %
504 %  A description of each parameter follows:
505 %
506 %    o severity: the severity of the exception.
507 %
508 %    o tag: the message tag.
509 %
510 */
511
512 static const char *ExceptionSeverityToTag(const ExceptionType severity)
513 {
514   switch (severity)
515   {
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/");
579     default: break;
580   }
581   return("");
582 }
583
584 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
585   const char *tag)
586 {
587   char
588     message[MagickPathExtent];
589
590   const char
591     *locale_message;
592
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)
598     return(tag);
599   if (locale_message == message)
600     return(tag);
601   return(locale_message);
602 }
603 \f
604 /*
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 %                                                                             %
607 %                                                                             %
608 %                                                                             %
609 %   I n h e r i t E x c e p t i o n                                           %
610 %                                                                             %
611 %                                                                             %
612 %                                                                             %
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 %
615 %  InheritException() inherits an exception from a related exception.
616 %
617 %  The format of the InheritException method is:
618 %
619 %      InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
620 %
621 %  A description of each parameter follows:
622 %
623 %    o exception: the exception info.
624 %
625 %    o relative: the related exception info.
626 %
627 */
628 MagickExport void InheritException(ExceptionInfo *exception,
629   const ExceptionInfo *relative)
630 {
631   register const ExceptionInfo
632     *p;
633
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)
640     return;
641   LockSemaphoreInfo(relative->semaphore);
642   ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
643   p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
644     relative->exceptions);
645   while (p != (const ExceptionInfo *) NULL)
646   {
647     (void) ThrowException(exception,p->severity,p->reason,p->description);
648     p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
649       relative->exceptions);
650   }
651   UnlockSemaphoreInfo(relative->semaphore);
652 }
653 \f
654 /*
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 %                                                                             %
657 %                                                                             %
658 %                                                                             %
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                           %
660 %                                                                             %
661 %                                                                             %
662 %                                                                             %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 %
665 %  InitializeExceptionInfo() initializes an exception to default values.
666 %
667 %  The format of the InitializeExceptionInfo method is:
668 %
669 %      InitializeExceptionInfo(ExceptionInfo *exception)
670 %
671 %  A description of each parameter follows:
672 %
673 %    o exception: the exception info.
674 %
675 */
676 MagickPrivate void InitializeExceptionInfo(ExceptionInfo *exception)
677 {
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;
684 }
685 \f
686 /*
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 %                                                                             %
689 %                                                                             %
690 %                                                                             %
691 %   M a g i c k E r r o r                                                     %
692 %                                                                             %
693 %                                                                             %
694 %                                                                             %
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696 %
697 %  MagickError() calls the exception handler methods with an error reason.
698 %
699 %  The format of the MagickError method is:
700 %
701 %      void MagickError(const ExceptionType error,const char *reason,
702 %        const char *description)
703 %
704 %  A description of each parameter follows:
705 %
706 %    o exception: Specifies the numeric error category.
707 %
708 %    o reason: Specifies the reason to display before terminating the
709 %      program.
710 %
711 %    o description: Specifies any description to the reason.
712 %
713 */
714 MagickExport void MagickError(const ExceptionType error,const char *reason,
715   const char *description)
716 {
717   if (error_handler != (ErrorHandler) NULL)
718     (*error_handler)(error,reason,description);
719 }
720 \f
721 /*
722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 %                                                                             %
724 %                                                                             %
725 %                                                                             %
726 %   M a g i c k F a t al E r r o r                                            %
727 %                                                                             %
728 %                                                                             %
729 %                                                                             %
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 %
732 %  MagickFatalError() calls the fatal exception handler methods with an error
733 %  reason.
734 %
735 %  The format of the MagickError method is:
736 %
737 %      void MagickFatalError(const ExceptionType error,const char *reason,
738 %        const char *description)
739 %
740 %  A description of each parameter follows:
741 %
742 %    o exception: Specifies the numeric error category.
743 %
744 %    o reason: Specifies the reason to display before terminating the
745 %      program.
746 %
747 %    o description: Specifies any description to the reason.
748 %
749 */
750 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
751   const char *description)
752 {
753   if (fatal_error_handler != (ErrorHandler) NULL)
754     (*fatal_error_handler)(error,reason,description);
755 }
756 \f
757 /*
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 %                                                                             %
760 %                                                                             %
761 %                                                                             %
762 %   M a g i c k W a r n i n g                                                 %
763 %                                                                             %
764 %                                                                             %
765 %                                                                             %
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 %
768 %  MagickWarning() calls the warning handler methods with a warning reason.
769 %
770 %  The format of the MagickWarning method is:
771 %
772 %      void MagickWarning(const ExceptionType warning,const char *reason,
773 %        const char *description)
774 %
775 %  A description of each parameter follows:
776 %
777 %    o warning: the warning severity.
778 %
779 %    o reason: Define the reason for the warning.
780 %
781 %    o description: Describe the warning.
782 %
783 */
784 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
785   const char *description)
786 {
787   if (warning_handler != (WarningHandler) NULL)
788     (*warning_handler)(warning,reason,description);
789 }
790 \f
791 /*
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 %                                                                             %
794 %                                                                             %
795 %                                                                             %
796 %   S e t E r r o r H a n d l e r                                             %
797 %                                                                             %
798 %                                                                             %
799 %                                                                             %
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 %
802 %  SetErrorHandler() sets the exception handler to the specified method
803 %  and returns the previous exception handler.
804 %
805 %  The format of the SetErrorHandler method is:
806 %
807 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
808 %
809 %  A description of each parameter follows:
810 %
811 %    o handler: the method to handle errors.
812 %
813 */
814 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
815 {
816   ErrorHandler
817     previous_handler;
818
819   previous_handler=error_handler;
820   error_handler=handler;
821   return(previous_handler);
822 }
823 \f
824 /*
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 %                                                                             %
827 %                                                                             %
828 %                                                                             %
829 %   S e t F a t a l E r r o r H a n d l e r                                   %
830 %                                                                             %
831 %                                                                             %
832 %                                                                             %
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 %
835 %  SetFatalErrorHandler() sets the fatal exception handler to the specified
836 %  method and returns the previous fatal exception handler.
837 %
838 %  The format of the SetErrorHandler method is:
839 %
840 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
841 %
842 %  A description of each parameter follows:
843 %
844 %    o handler: the method to handle errors.
845 %
846 */
847 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
848 {
849   FatalErrorHandler
850     previous_handler;
851
852   previous_handler=fatal_error_handler;
853   fatal_error_handler=handler;
854   return(previous_handler);
855 }
856 \f
857 /*
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 %                                                                             %
860 %                                                                             %
861 %                                                                             %
862 %   S e t W a r n i n g H a n d l e r                                         %
863 %                                                                             %
864 %                                                                             %
865 %                                                                             %
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 %
868 %  SetWarningHandler() sets the warning handler to the specified method
869 %  and returns the previous warning handler.
870 %
871 %  The format of the SetWarningHandler method is:
872 %
873 %      ErrorHandler SetWarningHandler(ErrorHandler handler)
874 %
875 %  A description of each parameter follows:
876 %
877 %    o handler: the method to handle warnings.
878 %
879 */
880 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
881 {
882   WarningHandler
883     previous_handler;
884
885   previous_handler=warning_handler;
886   warning_handler=handler;
887   return(previous_handler);
888 }
889 \f
890 /*
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 %                                                                             %
893 %                                                                             %
894 %                                                                             %
895 %   T h r o w E x c e p t i o n                                               %
896 %                                                                             %
897 %                                                                             %
898 %                                                                             %
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
900 %
901 %  ThrowException() throws an exception with the specified severity code,
902 %  reason, and optional description.
903 %
904 %  The format of the ThrowException method is:
905 %
906 %      MagickBooleanType ThrowException(ExceptionInfo *exception,
907 %        const ExceptionType severity,const char *reason,
908 %        const char *description)
909 %
910 %  A description of each parameter follows:
911 %
912 %    o exception: the exception info.
913 %
914 %    o severity: the severity of the exception.
915 %
916 %    o reason: the reason for the exception.
917 %
918 %    o description: the exception description.
919 %
920 */
921 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
922   const ExceptionType severity,const char *reason,const char *description)
923 {
924   LinkedListInfo
925     *exceptions;
926
927   register ExceptionInfo
928     *p;
929
930   assert(exception != (ExceptionInfo *) NULL);
931   assert(exception->signature == MagickCoreSignature);
932   LockSemaphoreInfo(exception->semaphore);
933   exceptions=(LinkedListInfo *) exception->exceptions;
934   if (GetNumberOfElementsInLinkedList(exceptions) > MaxExceptionList)
935     {
936       UnlockSemaphoreInfo(exception->semaphore);
937       return(MagickTrue);
938     }
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))
943     {
944       UnlockSemaphoreInfo(exception->semaphore);
945       return(MagickTrue);
946     }
947   p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
948   if (p == (ExceptionInfo *) NULL)
949     {
950       UnlockSemaphoreInfo(exception->semaphore);
951       ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
952     }
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)
962     {
963       exception->severity=p->severity;
964       exception->reason=p->reason;
965       exception->description=p->description;
966     }
967   UnlockSemaphoreInfo(exception->semaphore);
968   if (GetNumberOfElementsInLinkedList(exceptions) == MaxExceptionList)
969     (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
970       "TooManyExceptions","(exception processing is suspended)");
971   return(MagickTrue);
972 }
973 \f
974 /*
975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 %                                                                             %
977 %                                                                             %
978 %                                                                             %
979 %   T h r o w M a g i c k E x c e p t i o n                                   %
980 %                                                                             %
981 %                                                                             %
982 %                                                                             %
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984 %
985 %  ThrowMagickException logs an exception as determined by the log
986 %  configuration file.  If an error occurs, MagickFalse is returned
987 %  otherwise MagickTrue.
988 %
989 %  The format of the ThrowMagickException method is:
990 %
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,...)
994 %
995 %  A description of each parameter follows:
996 %
997 %    o exception: the exception info.
998 %
999 %    o filename: the source module filename.
1000 %
1001 %    o function: the function name.
1002 %
1003 %    o line: the line number of the source module.
1004 %
1005 %    o severity: Specifies the numeric error category.
1006 %
1007 %    o tag: the locale tag.
1008 %
1009 %    o format: the output format.
1010 %
1011 */
1012
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)
1017 {
1018   char
1019     message[MagickPathExtent],
1020     path[MagickPathExtent],
1021     reason[MagickPathExtent];
1022
1023   const char
1024     *locale,
1025     *type;
1026
1027   int
1028     n;
1029
1030   MagickBooleanType
1031     status;
1032
1033   size_t
1034     length;
1035
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);
1044 #else
1045   n=vsprintf(reason+length,format,operands);
1046 #endif
1047   if (n < 0)
1048     reason[MagickPathExtent-1]='\0';
1049   status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
1050   GetPathComponent(module,TailPath,path);
1051   type="undefined";
1052   if ((severity >= WarningException) && (severity < ErrorException))
1053     type="warning";
1054   if ((severity >= ErrorException) && (severity < FatalErrorException))
1055     type="error";
1056   if (severity >= FatalErrorException)
1057     type="fatal";
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);
1061   return(status);
1062 }
1063
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,...)
1067 {
1068   MagickBooleanType
1069     status;
1070
1071   va_list
1072     operands;
1073
1074   va_start(operands,format);
1075   status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
1076     format,operands);
1077   va_end(operands);
1078   return(status);
1079 }