Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpng/pngerror.c
Line
Count
Source
1
/* pngerror.c - functions for warnings and error handling
2
 *
3
 * Copyright (c) 2018-2025 Cosmin Truta
4
 * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
5
 * Copyright (c) 1996-1997 Andreas Dilger
6
 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
 *
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
10
 * and license in png.h
11
 *
12
 * This file provides a location for all error handling.  Users who
13
 * need special error handling are expected to write replacement functions
14
 * and use png_set_error_fn() to use those functions.  See the instructions
15
 * at each function.
16
 */
17
18
#include "pngpriv.h"
19
20
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
21
22
#define png_isalpha(c) \
23
4.07M
   (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
24
25
#if defined(PNG_WARNINGS_SUPPORTED) || \
26
    (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)) || \
27
    defined(PNG_TIME_RFC1123_SUPPORTED)
28
static const char png_digits[] = {
29
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
30
   'A', 'B', 'C', 'D', 'E', 'F'
31
};
32
#endif
33
34
static PNG_FUNCTION(void /* PRIVATE */,
35
png_default_error,(const png_struct *png_ptr, const char *error_message),
36
    PNG_NORETURN);
37
38
#ifdef PNG_WARNINGS_SUPPORTED
39
static void /* PRIVATE */
40
png_default_warning(const png_struct *png_ptr,
41
    const char *warning_message);
42
#endif /* WARNINGS */
43
44
/* This function is called whenever there is a fatal error.  This function
45
 * should not be changed.  If there is a need to handle errors differently,
46
 * you should supply a replacement error function and use png_set_error_fn()
47
 * to replace the error function at run-time.
48
 */
49
#ifdef PNG_ERROR_TEXT_SUPPORTED
50
PNG_FUNCTION(void,
51
png_error,(const png_struct *png_ptr, const char *error_message),
52
    PNG_NORETURN)
53
13.2k
{
54
13.2k
   if (png_ptr != NULL && png_ptr->error_fn != NULL)
55
13.2k
      (*(png_ptr->error_fn))(png_constcast(png_struct *,png_ptr),
56
13.2k
          error_message);
57
58
   /* If the custom handler doesn't exist, or if it returns,
59
      use the default handler, which will not return. */
60
13.2k
   png_default_error(png_ptr, error_message);
61
13.2k
}
62
#else
63
PNG_FUNCTION(void,
64
png_err,(const png_struct *png_ptr),
65
    PNG_NORETURN)
66
{
67
   /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
68
    * erroneously as '\0', instead of the empty string "".  This was
69
    * apparently an error, introduced in libpng-1.2.20, and png_default_error
70
    * will crash in this case.
71
    */
72
   if (png_ptr != NULL && png_ptr->error_fn != NULL)
73
      (*(png_ptr->error_fn))(png_constcast(png_struct *,png_ptr), "");
74
75
   /* If the custom handler doesn't exist, or if it returns,
76
      use the default handler, which will not return. */
77
   png_default_error(png_ptr, "");
78
}
79
#endif /* ERROR_TEXT */
80
81
/* Utility to safely append strings to a buffer.  This never errors out so
82
 * error checking is not required in the caller.
83
 */
84
size_t
85
png_safecat(char *buffer, size_t bufsize, size_t pos,
86
    const char *string)
87
1.86M
{
88
1.86M
   if (buffer != NULL && pos < bufsize)
89
1.86M
   {
90
1.86M
      if (string != NULL)
91
21.9M
         while (*string != '\0' && pos < bufsize-1)
92
20.1M
           buffer[pos++] = *string++;
93
94
1.86M
      buffer[pos] = '\0';
95
1.86M
   }
96
97
1.86M
   return pos;
98
1.86M
}
99
100
#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
101
/* Utility to dump an unsigned value into a buffer, given a start pointer and
102
 * an end pointer (which should point just *beyond* the end of the buffer!).
103
 * Returns the pointer to the start of the formatted string.
104
 */
105
char *
106
png_format_number(const char *start, char *end, int format,
107
    png_alloc_size_t number)
108
298k
{
109
298k
   int count = 0;    /* number of digits output */
110
298k
   int mincount = 1; /* minimum number required */
111
298k
   int output = 0;   /* digit output (for the fixed point format) */
112
113
298k
   *--end = '\0';
114
115
   /* This is written so that the loop always runs at least once, even with
116
    * number zero.
117
    */
118
1.52M
   while (end > start && (number != 0 || count < mincount))
119
1.23M
   {
120
1.23M
      switch (format)
121
1.23M
      {
122
0
         case PNG_NUMBER_FORMAT_fixed:
123
            /* Needs five digits (the fraction) */
124
0
            mincount = 5;
125
0
            if (output != 0 || number % 10 != 0)
126
0
            {
127
0
               *--end = png_digits[number % 10];
128
0
               output = 1;
129
0
            }
130
0
            number /= 10;
131
0
            break;
132
133
0
         case PNG_NUMBER_FORMAT_02u:
134
            /* Expects at least 2 digits. */
135
0
            mincount = 2;
136
            /* FALLTHROUGH */
137
138
0
         case PNG_NUMBER_FORMAT_u:
139
0
            *--end = png_digits[number % 10];
140
0
            number /= 10;
141
0
            break;
142
143
550
         case PNG_NUMBER_FORMAT_02x:
144
            /* This format expects at least two digits */
145
550
            mincount = 2;
146
            /* FALLTHROUGH */
147
148
1.23M
         case PNG_NUMBER_FORMAT_x:
149
1.23M
            *--end = png_digits[number & 0xf];
150
1.23M
            number >>= 4;
151
1.23M
            break;
152
153
0
         default: /* an error */
154
0
            number = 0;
155
0
            break;
156
1.23M
      }
157
158
      /* Keep track of the number of digits added */
159
1.23M
      ++count;
160
161
      /* Float a fixed number here: */
162
1.23M
      if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start))
163
0
      {
164
         /* End of the fraction, but maybe nothing was output?  In that case
165
          * drop the decimal point.  If the number is a true zero handle that
166
          * here.
167
          */
168
0
         if (output != 0)
169
0
            *--end = '.';
170
0
         else if (number == 0) /* and !output */
171
0
            *--end = '0';
172
0
      }
173
1.23M
   }
174
175
298k
   return end;
176
298k
}
177
#endif
178
179
#ifdef PNG_WARNINGS_SUPPORTED
180
/* This function is called whenever there is a non-fatal error.  This function
181
 * should not be changed.  If there is a need to handle warnings differently,
182
 * you should supply a replacement warning function and use
183
 * png_set_error_fn() to replace the warning function at run-time.
184
 */
185
void
186
png_warning(const png_struct *png_ptr, const char *warning_message)
187
1.14M
{
188
1.14M
   if (png_ptr != NULL && png_ptr->warning_fn != NULL)
189
1.14M
      (*(png_ptr->warning_fn))(png_constcast(png_struct *,png_ptr),
190
1.14M
          warning_message);
191
0
   else
192
0
      png_default_warning(png_ptr, warning_message);
193
1.14M
}
194
195
/* These functions support 'formatted' warning messages with up to
196
 * PNG_WARNING_PARAMETER_COUNT parameters.  In the format string the parameter
197
 * is introduced by @<number>, where 'number' starts at 1.  This follows the
198
 * standard established by X/Open for internationalizable error messages.
199
 */
200
void
201
png_warning_parameter(png_warning_parameters p, int number,
202
    const char *string)
203
550
{
204
550
   if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
205
550
      (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
206
550
}
207
208
void
209
png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
210
    png_alloc_size_t value)
211
0
{
212
0
   char buffer[PNG_NUMBER_BUFFER_SIZE] = {0};
213
0
   png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
214
0
}
215
216
void
217
png_warning_parameter_signed(png_warning_parameters p, int number, int format,
218
    png_int_32 value)
219
275
{
220
275
   png_alloc_size_t u;
221
275
   char *str;
222
275
   char buffer[PNG_NUMBER_BUFFER_SIZE] = {0};
223
224
   /* Avoid overflow by doing the negate in a png_alloc_size_t: */
225
275
   u = (png_alloc_size_t)value;
226
275
   if (value < 0)
227
0
      u = ~u + 1;
228
229
275
   str = PNG_FORMAT_NUMBER(buffer, format, u);
230
231
275
   if (value < 0 && str > buffer)
232
0
      *--str = '-';
233
234
275
   png_warning_parameter(p, number, str);
235
275
}
236
237
void
238
png_formatted_warning(const png_struct *png_ptr, png_warning_parameters p,
239
    const char *message)
240
275
{
241
   /* The internal buffer is just 192 bytes - enough for all our messages,
242
    * overflow doesn't happen because this code checks!  If someone figures
243
    * out how to send us a message longer than 192 bytes, all that will
244
    * happen is that the message will be truncated appropriately.
245
    */
246
275
   size_t i = 0; /* Index in the msg[] buffer: */
247
275
   char msg[192];
248
249
   /* Iterate through characters in message and resolve encountered
250
    * parameters, which consist of @ followed by parameter number. Either
251
    * add the resolved parameter or the raw character at msg[i]. Always check
252
    * that there is still space for the trailing '\0'.
253
    */
254
9.07k
   while (i<(sizeof msg)-1 && *message != '\0')
255
8.80k
   {
256
      /* '@' at end of string is now just printed (previously it was skipped);
257
       * it is an error in the calling code to terminate the string with @.
258
       */
259
8.80k
      if (p != NULL && *message == '@' && message[1] != '\0')
260
550
      {
261
550
         const int parameter_char = *++message; /* Consume the '@' */
262
263
         /* If the parameter digit is out of range it will just get printed. */
264
550
         if (parameter_char >= '1' && parameter_char <= '9')
265
550
         {
266
550
            const int parameter = parameter_char - '1';
267
268
            /* Append this parameter */
269
550
            const char *parm = p[parameter];
270
550
            const char *pend = p[parameter] + (sizeof p[parameter]);
271
272
            /* No need to copy the trailing '\0' here, but there is no guarantee
273
             * that parm[] has been initialized, so there is no guarantee of a
274
             * trailing '\0':
275
             */
276
1.68k
            while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
277
1.13k
               msg[i++] = *parm++;
278
279
            /* Consume the parameter digit too: */
280
550
            ++message;
281
550
            continue;
282
550
         }
283
284
         /* else not a parameter and there is a character after the @ sign; just
285
          * copy that.  This is known not to be '\0' because of the test above.
286
          */
287
550
      }
288
289
      /* At this point *message can't be '\0', even in the bad parameter case
290
       * above where there is a lone '@' at the end of the message string.
291
       */
292
8.25k
      msg[i++] = *message++;
293
8.25k
   }
294
295
   /* i is always less than (sizeof msg), so: */
296
275
   msg[i] = '\0';
297
298
   /* And this is the formatted message. It may be larger than
299
    * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these
300
    * are not (currently) formatted.
301
    */
302
275
   png_warning(png_ptr, msg);
303
275
}
304
#endif /* WARNINGS */
305
306
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
307
void
308
png_benign_error(const png_struct *png_ptr, const char *error_message)
309
2.01k
{
310
2.01k
   if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
311
2.01k
   {
312
2.01k
#     ifdef PNG_READ_SUPPORTED
313
2.01k
         if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
314
2.01k
            png_ptr->chunk_name != 0)
315
2.01k
            png_chunk_warning(png_ptr, error_message);
316
0
         else
317
0
#     endif
318
0
      png_warning(png_ptr, error_message);
319
2.01k
   }
320
321
0
   else
322
0
   {
323
0
#     ifdef PNG_READ_SUPPORTED
324
0
         if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
325
0
            png_ptr->chunk_name != 0)
326
0
            png_chunk_error(png_ptr, error_message);
327
0
         else
328
0
#     endif
329
0
      png_error(png_ptr, error_message);
330
0
   }
331
332
#  ifndef PNG_ERROR_TEXT_SUPPORTED
333
      PNG_UNUSED(error_message)
334
#  endif
335
2.01k
}
336
337
void /* PRIVATE */
338
png_app_warning(const png_struct *png_ptr, const char *error_message)
339
83.0k
{
340
83.0k
   if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0)
341
83.0k
      png_warning(png_ptr, error_message);
342
0
   else
343
0
      png_error(png_ptr, error_message);
344
345
#  ifndef PNG_ERROR_TEXT_SUPPORTED
346
      PNG_UNUSED(error_message)
347
#  endif
348
83.0k
}
349
350
void /* PRIVATE */
351
png_app_error(const png_struct *png_ptr, const char *error_message)
352
0
{
353
0
   if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0)
354
0
      png_warning(png_ptr, error_message);
355
0
   else
356
0
      png_error(png_ptr, error_message);
357
358
#  ifndef PNG_ERROR_TEXT_SUPPORTED
359
      PNG_UNUSED(error_message)
360
#  endif
361
0
}
362
#endif /* BENIGN_ERRORS */
363
364
29.7M
#define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */
365
#if defined(PNG_WARNINGS_SUPPORTED) || \
366
   (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
367
/* These utilities are used internally to build an error message that relates
368
 * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
369
 * which is used to prefix the message.  The message is limited in length
370
 * to 63 bytes. The name characters are output as hex digits wrapped in []
371
 * if the character is invalid.
372
 */
373
static void /* PRIVATE */
374
png_format_buffer(const png_struct *png_ptr, char *buffer,
375
    const char *error_message)
376
1.01M
{
377
1.01M
   png_uint_32 chunk_name = png_ptr->chunk_name;
378
1.01M
   int iout = 0, ishift;
379
380
5.09M
   for (ishift = 24; ishift >= 0; ishift -= 8)
381
4.07M
   {
382
4.07M
      int c = (int)(chunk_name >> ishift) & 0xff;
383
384
4.07M
      if (!png_isalpha(c))
385
10.5k
      {
386
10.5k
         buffer[iout++] = '[';
387
10.5k
         buffer[iout++] = png_digits[(c & 0xf0) >> 4];
388
10.5k
         buffer[iout++] = png_digits[c & 0x0f];
389
10.5k
         buffer[iout++] = ']';
390
10.5k
      }
391
392
4.06M
      else
393
4.06M
      {
394
4.06M
         buffer[iout++] = (char)c;
395
4.06M
      }
396
4.07M
   }
397
398
1.01M
   if (error_message == NULL)
399
0
      buffer[iout] = '\0';
400
401
1.01M
   else
402
1.01M
   {
403
1.01M
      int iin = 0;
404
405
1.01M
      buffer[iout++] = ':';
406
1.01M
      buffer[iout++] = ' ';
407
408
29.7M
      while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
409
28.7M
         buffer[iout++] = error_message[iin++];
410
411
      /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
412
1.01M
      buffer[iout] = '\0';
413
1.01M
   }
414
1.01M
}
415
#endif /* WARNINGS || ERROR_TEXT */
416
417
#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
418
PNG_FUNCTION(void,
419
png_chunk_error,(const png_struct *png_ptr, const char *error_message),
420
    PNG_NORETURN)
421
8.44k
{
422
8.44k
   char msg[18+PNG_MAX_ERROR_TEXT];
423
8.44k
   if (png_ptr == NULL)
424
0
      png_error(png_ptr, error_message);
425
426
8.44k
   else
427
8.44k
   {
428
8.44k
      png_format_buffer(png_ptr, msg, error_message);
429
8.44k
      png_error(png_ptr, msg);
430
8.44k
   }
431
8.44k
}
432
#endif /* READ && ERROR_TEXT */
433
434
#ifdef PNG_WARNINGS_SUPPORTED
435
void
436
png_chunk_warning(const png_struct *png_ptr, const char *warning_message)
437
1.01M
{
438
1.01M
   char msg[18+PNG_MAX_ERROR_TEXT];
439
1.01M
   if (png_ptr == NULL)
440
0
      png_warning(png_ptr, warning_message);
441
442
1.01M
   else
443
1.01M
   {
444
1.01M
      png_format_buffer(png_ptr, msg, warning_message);
445
1.01M
      png_warning(png_ptr, msg);
446
1.01M
   }
447
1.01M
}
448
#endif /* WARNINGS */
449
450
#ifdef PNG_READ_SUPPORTED
451
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
452
void
453
png_chunk_benign_error(const png_struct *png_ptr,
454
    const char *error_message)
455
470k
{
456
470k
   if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
457
470k
      png_chunk_warning(png_ptr, error_message);
458
459
0
   else
460
0
      png_chunk_error(png_ptr, error_message);
461
462
#  ifndef PNG_ERROR_TEXT_SUPPORTED
463
      PNG_UNUSED(error_message)
464
#  endif
465
470k
}
466
#endif
467
#endif /* READ */
468
469
void /* PRIVATE */
470
png_chunk_report(const png_struct *png_ptr, const char *message, int error)
471
141
{
472
#  ifndef PNG_WARNINGS_SUPPORTED
473
      PNG_UNUSED(message)
474
#  endif
475
476
   /* This is always supported, but for just read or just write it
477
    * unconditionally does the right thing.
478
    */
479
141
#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
480
141
      if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
481
141
#  endif
482
483
141
#  ifdef PNG_READ_SUPPORTED
484
141
      {
485
141
         if (error < PNG_CHUNK_ERROR)
486
141
            png_chunk_warning(png_ptr, message);
487
488
0
         else
489
0
            png_chunk_benign_error(png_ptr, message);
490
141
      }
491
0
#  endif
492
493
0
#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
494
0
      else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
495
0
#  endif
496
497
0
#  ifdef PNG_WRITE_SUPPORTED
498
0
      {
499
0
         if (error < PNG_CHUNK_WRITE_ERROR)
500
0
            png_app_warning(png_ptr, message);
501
502
0
         else
503
0
            png_app_error(png_ptr, message);
504
0
      }
505
141
#  endif
506
141
}
507
508
#ifdef PNG_ERROR_TEXT_SUPPORTED
509
#ifdef PNG_FLOATING_POINT_SUPPORTED
510
PNG_FUNCTION(void,
511
png_fixed_error,(const png_struct *png_ptr, const char *name),
512
    PNG_NORETURN)
513
7
{
514
99
#  define fixed_message "fixed point overflow in "
515
92
#  define fixed_message_ln ((sizeof fixed_message)-1)
516
7
   unsigned int  iin;
517
7
   char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
518
7
   memcpy(msg, fixed_message, fixed_message_ln);
519
7
   iin = 0;
520
7
   if (name != NULL)
521
85
      while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
522
78
      {
523
78
         msg[fixed_message_ln + iin] = name[iin];
524
78
         ++iin;
525
78
      }
526
7
   msg[fixed_message_ln + iin] = 0;
527
7
   png_error(png_ptr, msg);
528
7
}
529
#endif
530
#endif
531
532
#ifdef PNG_SETJMP_SUPPORTED
533
/* This API only exists if ANSI-C style error handling is used,
534
 * otherwise it is necessary for png_default_error to be overridden.
535
 */
536
jmp_buf*
537
png_set_longjmp_fn(png_struct *png_ptr, png_longjmp_ptr longjmp_fn,
538
    size_t jmp_buf_size)
539
96.0k
{
540
   /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
541
    * and it must not change after that.  Libpng doesn't care how big the
542
    * buffer is, just that it doesn't change.
543
    *
544
    * If the buffer size is no *larger* than the size of jmp_buf when libpng is
545
    * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
546
    * semantics that this call will not fail.  If the size is larger, however,
547
    * the buffer is allocated and this may fail, causing the function to return
548
    * NULL.
549
    */
550
96.0k
   if (png_ptr == NULL)
551
0
      return NULL;
552
553
96.0k
   if (png_ptr->jmp_buf_ptr == NULL)
554
96.0k
   {
555
96.0k
      png_ptr->jmp_buf_size = 0; /* not allocated */
556
557
96.0k
      if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local))
558
96.0k
         png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
559
560
0
      else
561
0
      {
562
0
         png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
563
0
             png_malloc_warn(png_ptr, jmp_buf_size));
564
565
0
         if (png_ptr->jmp_buf_ptr == NULL)
566
0
            return NULL; /* new NULL return on OOM */
567
568
0
         png_ptr->jmp_buf_size = jmp_buf_size;
569
0
      }
570
96.0k
   }
571
572
0
   else /* Already allocated: check the size */
573
0
   {
574
0
      size_t size = png_ptr->jmp_buf_size;
575
576
0
      if (size == 0)
577
0
      {
578
0
         size = (sizeof png_ptr->jmp_buf_local);
579
0
         if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
580
0
         {
581
            /* This is an internal error in libpng: somehow we have been left
582
             * with a stack allocated jmp_buf when the application regained
583
             * control.  It's always possible to fix this up, but for the moment
584
             * this is a png_error because that makes it easy to detect.
585
             */
586
0
            png_error(png_ptr, "Libpng jmp_buf still allocated");
587
            /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
588
0
         }
589
0
      }
590
591
0
      if (size != jmp_buf_size)
592
0
      {
593
0
         png_warning(png_ptr, "Application jmp_buf size changed");
594
0
         return NULL; /* caller will probably crash: no choice here */
595
0
      }
596
0
   }
597
598
   /* Finally fill in the function, now we have a satisfactory buffer. It is
599
    * valid to change the function on every call.
600
    */
601
96.0k
   png_ptr->longjmp_fn = longjmp_fn;
602
96.0k
   return png_ptr->jmp_buf_ptr;
603
96.0k
}
604
605
void /* PRIVATE */
606
png_free_jmpbuf(png_struct *png_ptr)
607
96.0k
{
608
96.0k
   if (png_ptr != NULL)
609
96.0k
   {
610
96.0k
      jmp_buf *jb = png_ptr->jmp_buf_ptr;
611
612
      /* A size of 0 is used to indicate a local, stack, allocation of the
613
       * pointer; used here and in png.c
614
       */
615
96.0k
      if (jb != NULL && png_ptr->jmp_buf_size > 0)
616
0
      {
617
618
         /* This stuff is so that a failure to free the error control structure
619
          * does not leave libpng in a state with no valid error handling: the
620
          * free always succeeds, if there is an error it gets ignored.
621
          */
622
0
         if (jb != &png_ptr->jmp_buf_local)
623
0
         {
624
            /* Make an internal, libpng, jmp_buf to return here */
625
0
            jmp_buf free_jmp_buf;
626
627
0
            if (!setjmp(free_jmp_buf))
628
0
            {
629
0
               png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
630
0
               png_ptr->jmp_buf_size = 0; /* stack allocation */
631
0
               png_ptr->longjmp_fn = longjmp;
632
0
               png_free(png_ptr, jb); /* Return to setjmp on error */
633
0
            }
634
0
         }
635
0
      }
636
637
      /* *Always* cancel everything out: */
638
96.0k
      png_ptr->jmp_buf_size = 0;
639
96.0k
      png_ptr->jmp_buf_ptr = NULL;
640
96.0k
      png_ptr->longjmp_fn = 0;
641
96.0k
   }
642
96.0k
}
643
#endif
644
645
/* This is the default error handling function.  Note that replacements for
646
 * this function MUST NOT RETURN, or the program will likely crash.  This
647
 * function is used by default, or if the program supplies NULL for the
648
 * error function pointer in png_set_error_fn().
649
 */
650
static PNG_FUNCTION(void /* PRIVATE */,
651
png_default_error,(const png_struct *png_ptr, const char *error_message),
652
    PNG_NORETURN)
653
0
{
654
0
#ifdef PNG_CONSOLE_IO_SUPPORTED
655
0
   fprintf(stderr, "libpng error: %s\n", error_message ? error_message :
656
0
      "undefined");
657
#else
658
   PNG_UNUSED(error_message) /* Make compiler happy */
659
#endif
660
0
   png_longjmp(png_ptr, 1);
661
0
}
662
663
PNG_FUNCTION(void,
664
png_longjmp,(const png_struct *png_ptr, int val),
665
    PNG_NORETURN)
666
56.0k
{
667
56.0k
#ifdef PNG_SETJMP_SUPPORTED
668
56.0k
   if (png_ptr != NULL && png_ptr->longjmp_fn != NULL &&
669
56.0k
       png_ptr->jmp_buf_ptr != NULL)
670
56.0k
      png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
671
#else
672
   PNG_UNUSED(png_ptr)
673
   PNG_UNUSED(val)
674
#endif
675
676
   /* If control reaches this point, png_longjmp() must not return. The only
677
    * choice is to terminate the whole process (or maybe the thread); to do
678
    * this the ANSI-C abort() function is used unless a different method is
679
    * implemented by overriding the default configuration setting for
680
    * PNG_ABORT().
681
    */
682
56.0k
   PNG_ABORT();
683
56.0k
}
684
685
#ifdef PNG_WARNINGS_SUPPORTED
686
/* This function is called when there is a warning, but the library thinks
687
 * it can continue anyway.  Replacement functions don't have to do anything
688
 * here if you don't want them to.  In the default configuration, png_ptr is
689
 * not used, but it is passed in case it may be useful.
690
 */
691
static void /* PRIVATE */
692
png_default_warning(const png_struct *png_ptr, const char *warning_message)
693
0
{
694
0
#ifdef PNG_CONSOLE_IO_SUPPORTED
695
0
   fprintf(stderr, "libpng warning: %s\n", warning_message);
696
#else
697
   PNG_UNUSED(warning_message) /* Make compiler happy */
698
#endif
699
0
   PNG_UNUSED(png_ptr) /* Make compiler happy */
700
0
}
701
#endif /* WARNINGS */
702
703
/* This function is called when the application wants to use another method
704
 * of handling errors and warnings.  Note that the error function MUST NOT
705
 * return to the calling routine or serious problems will occur.  The return
706
 * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1).
707
 */
708
void
709
png_set_error_fn(png_struct *png_ptr, void *error_ptr,
710
    png_error_ptr error_fn, png_error_ptr warning_fn)
711
96.0k
{
712
96.0k
   if (png_ptr == NULL)
713
0
      return;
714
715
96.0k
   png_ptr->error_ptr = error_ptr;
716
96.0k
   png_ptr->error_fn = error_fn;
717
96.0k
#ifdef PNG_WARNINGS_SUPPORTED
718
96.0k
   png_ptr->warning_fn = warning_fn;
719
#else
720
   PNG_UNUSED(warning_fn)
721
#endif
722
96.0k
}
723
724
725
/* This function returns a pointer to the error_ptr associated with the user
726
 * functions.  The application should free any memory associated with this
727
 * pointer before png_write_destroy and png_read_destroy are called.
728
 */
729
void *
730
png_get_error_ptr(const png_struct *png_ptr)
731
1.41M
{
732
1.41M
   if (png_ptr == NULL)
733
0
      return NULL;
734
735
1.41M
   return (void *)png_ptr->error_ptr;
736
1.41M
}
737
738
739
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
740
   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
741
   /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
742
    * possible to implement without setjmp support just so long as there is some
743
    * way to handle the error return here:
744
    */
745
PNG_FUNCTION(void /* PRIVATE */,
746
png_safe_error,(png_struct *png_nonconst_ptr, const char *error_message),
747
    PNG_NORETURN)
748
0
{
749
0
   const png_struct *png_ptr = png_nonconst_ptr;
750
0
   png_image *image = png_voidcast(png_image *, png_ptr->error_ptr);
751
752
   /* An error is always logged here, overwriting anything (typically a warning)
753
    * that is already there:
754
    */
755
0
   if (image != NULL)
756
0
   {
757
0
      png_safecat(image->message, (sizeof image->message), 0, error_message);
758
0
      image->warning_or_error |= PNG_IMAGE_ERROR;
759
760
      /* Retrieve the jmp_buf from within the png_control, making this work for
761
       * C++ compilation too is pretty tricky: C++ wants a pointer to the first
762
       * element of a jmp_buf, but C doesn't tell us the type of that.
763
       */
764
0
      if (image->opaque != NULL && image->opaque->error_buf != NULL)
765
0
         longjmp(png_control_jmp_buf(image->opaque), 1);
766
767
      /* Missing longjmp buffer, the following is to help debugging: */
768
0
      {
769
0
         size_t pos = png_safecat(image->message, (sizeof image->message), 0,
770
0
             "bad longjmp: ");
771
0
         png_safecat(image->message, (sizeof image->message), pos,
772
0
             error_message);
773
0
      }
774
0
   }
775
776
   /* Here on an internal programming error. */
777
0
   abort();
778
0
}
779
780
#ifdef PNG_WARNINGS_SUPPORTED
781
void /* PRIVATE */
782
png_safe_warning(png_struct *png_nonconst_ptr, const char *warning_message)
783
0
{
784
0
   const png_struct *png_ptr = png_nonconst_ptr;
785
0
   png_image *image = png_voidcast(png_image *, png_ptr->error_ptr);
786
787
   /* A warning is only logged if there is no prior warning or error. */
788
0
   if (image->warning_or_error == 0)
789
0
   {
790
0
      png_safecat(image->message, (sizeof image->message), 0, warning_message);
791
0
      image->warning_or_error |= PNG_IMAGE_WARNING;
792
0
   }
793
0
}
794
#endif
795
796
int /* PRIVATE */
797
png_safe_execute(png_image *image, int (*function)(void *), void *arg)
798
0
{
799
0
   void *saved_error_buf = image->opaque->error_buf;
800
0
   jmp_buf safe_jmpbuf;
801
802
   /* Safely execute function(arg), with png_error returning back here. */
803
0
   if (setjmp(safe_jmpbuf) == 0)
804
0
   {
805
0
      int result;
806
807
0
      image->opaque->error_buf = safe_jmpbuf;
808
0
      result = function(arg);
809
0
      image->opaque->error_buf = saved_error_buf;
810
811
0
      if (result)
812
0
         return 1; /* success */
813
0
   }
814
815
   /* The function failed either because of a caught png_error and a regular
816
    * return of false above or because of an uncaught png_error from the
817
    * function itself.  Ensure that the error_buf is always set back to the
818
    * value saved above:
819
    */
820
0
   image->opaque->error_buf = saved_error_buf;
821
822
   /* On the final false return, when about to return control to the caller, the
823
    * image is freed (png_image_free does this check but it is duplicated here
824
    * for clarity:
825
    */
826
0
   if (saved_error_buf == NULL)
827
0
      png_image_free(image);
828
829
0
   return 0; /* failure */
830
0
}
831
#endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */
832
#endif /* READ || WRITE */