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