Coverage Report

Created: 2025-10-12 07:48

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