/* pngerror.c - stub functions for i/o and memory allocation * * Copyright (c) 2018-2024 Cosmin Truta * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson * Copyright (c) 1996-1997 Andreas Dilger * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * This file provides a location for all error handling. Users who * need special error handling are expected to write replacement functions * and use png_set_error_fn() to use those functions. See the instructions * at each function. */ #include "pngpriv.h" #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr, png_const_charp error_message)),PNG_NORETURN); #ifdef PNG_WARNINGS_SUPPORTED static void /* PRIVATE */ png_default_warning PNGARG((png_const_structrp png_ptr, png_const_charp warning_message)); #endif /* WARNINGS */ /* This function is called whenever there is a fatal error. This function * should not be changed. If there is a need to handle errors differently, * you should supply a replacement error function and use png_set_error_fn() * to replace the error function at run-time. */ #ifdef PNG_ERROR_TEXT_SUPPORTED PNG_FUNCTION(…) { … #else PNG_FUNCTION(void,PNGAPI png_err,(png_const_structrp png_ptr),PNG_NORETURN) { /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed * erroneously as '\0', instead of the empty string "". This was * apparently an error, introduced in libpng-1.2.20, and png_default_error * will crash in this case. */ if (png_ptr != NULL && png_ptr->error_fn != NULL) (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), ""); /* If the custom handler doesn't exist, or if it returns, use the default handler, which will not return. */ png_default_error(png_ptr, ""); } #endif /* ERROR_TEXT */ /* Utility to safely appends strings to a buffer. This never errors out so * error checking is not required in the caller. */ size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string) { … } #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED) /* Utility to dump an unsigned value into a buffer, given a start pointer and * and end pointer (which should point just *beyond* the end of the buffer!) * Returns the pointer to the start of the formatted string. */ png_charp png_format_number(png_const_charp start, png_charp end, int format, png_alloc_size_t number) { … } #endif #ifdef PNG_WARNINGS_SUPPORTED /* This function is called whenever there is a non-fatal error. This function * should not be changed. If there is a need to handle warnings differently, * you should supply a replacement warning function and use * png_set_error_fn() to replace the warning function at run-time. */ void PNGAPI png_warning(png_const_structrp png_ptr, png_const_charp warning_message) { … } /* These functions support 'formatted' warning messages with up to * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter * is introduced by @<number>, where 'number' starts at 1. This follows the * standard established by X/Open for internationalizable error messages. */ void png_warning_parameter(png_warning_parameters p, int number, png_const_charp string) { … } void png_warning_parameter_unsigned(png_warning_parameters p, int number, int format, png_alloc_size_t value) { … } void png_warning_parameter_signed(png_warning_parameters p, int number, int format, png_int_32 value) { … } void png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p, png_const_charp message) { … } #endif /* WARNINGS */ #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI png_benign_error(png_const_structrp png_ptr, png_const_charp error_message) { … } void /* PRIVATE */ png_app_warning(png_const_structrp png_ptr, png_const_charp error_message) { … } void /* PRIVATE */ png_app_error(png_const_structrp png_ptr, png_const_charp error_message) { … } #endif /* BENIGN_ERRORS */ #define PNG_MAX_ERROR_TEXT … #if defined(PNG_WARNINGS_SUPPORTED) || \ (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)) /* These utilities are used internally to build an error message that relates * to the current chunk. The chunk name comes from png_ptr->chunk_name, * which is used to prefix the message. The message is limited in length * to 63 bytes. The name characters are output as hex digits wrapped in [] * if the character is invalid. */ #define isnonalpha(c) … static const char png_digit[16] = …; static void /* PRIVATE */ png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp error_message) { … } #endif /* WARNINGS || ERROR_TEXT */ #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED) PNG_FUNCTION(void,PNGAPI png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message), PNG_NORETURN) { … } #endif /* READ && ERROR_TEXT */ #ifdef PNG_WARNINGS_SUPPORTED void PNGAPI png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message) { … } #endif /* WARNINGS */ #ifdef PNG_READ_SUPPORTED #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp error_message) { … } #endif #endif /* READ */ void /* PRIVATE */ png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error) { … } #ifdef PNG_ERROR_TEXT_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED PNG_FUNCTION(void, png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN) { … } #endif #endif #ifdef PNG_SETJMP_SUPPORTED /* This API only exists if ANSI-C style error handling is used, * otherwise it is necessary for png_default_error to be overridden. */ jmp_buf* PNGAPI png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn, size_t jmp_buf_size) { … } void /* PRIVATE */ png_free_jmpbuf(png_structrp png_ptr) { … } #endif /* This is the default error handling function. Note that replacements for * this function MUST NOT RETURN, or the program will likely crash. This * function is used by default, or if the program supplies NULL for the * error function pointer in png_set_error_fn(). */ static PNG_FUNCTION(void /* PRIVATE */, png_default_error,(png_const_structrp png_ptr, png_const_charp error_message), PNG_NORETURN) { … } PNG_FUNCTION(…) { … #ifdef PNG_WARNINGS_SUPPORTED /* This function is called when there is a warning, but the library thinks * it can continue anyway. Replacement functions don't have to do anything * here if you don't want them to. In the default configuration, png_ptr is * not used, but it is passed in case it may be useful. */ static void /* PRIVATE */ png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message) { … } #endif /* WARNINGS */ /* This function is called when the application wants to use another method * of handling errors and warnings. Note that the error function MUST NOT * return to the calling routine or serious problems will occur. The return * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1) */ void PNGAPI png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn) { … } /* This function returns a pointer to the error_ptr associated with the user * functions. The application should free any memory associated with this * pointer before png_write_destroy and png_read_destroy are called. */ png_voidp PNGAPI png_get_error_ptr(png_const_structrp png_ptr) { … } #ifdef PNG_ERROR_NUMBERS_SUPPORTED void PNGAPI png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode) { if (png_ptr != NULL) { png_ptr->flags &= ((~(PNG_FLAG_STRIP_ERROR_NUMBERS | PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); } } #endif #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) /* Currently the above both depend on SETJMP_SUPPORTED, however it would be * possible to implement without setjmp support just so long as there is some * way to handle the error return here: */ PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message), PNG_NORETURN) { … } #ifdef PNG_WARNINGS_SUPPORTED void /* PRIVATE */ PNGCBAPI png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message) { … } #endif int /* PRIVATE */ png_safe_execute(png_imagep image, int (*function)(png_voidp), png_voidp arg) { … } #endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */ #endif /* READ || WRITE */