/* pngmem.c - stub functions for memory allocation1*2* Copyright (c) 2018-2025 Cosmin Truta3* Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson4* Copyright (c) 1996-1997 Andreas Dilger5* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.6*7* This code is released under the libpng license.8* For conditions of distribution and use, see the disclaimer9* and license in png.h10*11* This file provides a location for all memory allocation. Users who12* need special memory handling are expected to supply replacement13* functions for png_malloc() and png_free(), and to use14* png_create_read_struct_2() and png_create_write_struct_2() to15* identify the replacement functions.16*/1718#include "pngpriv.h"1920#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)21/* Free a png_struct */22void /* PRIVATE */23png_destroy_png_struct(png_structrp png_ptr)24{25if (png_ptr != NULL)26{27/* png_free might call png_error and may certainly call28* png_get_mem_ptr, so fake a temporary png_struct to support this.29*/30png_struct dummy_struct = *png_ptr;31memset(png_ptr, 0, (sizeof *png_ptr));32png_free(&dummy_struct, png_ptr);3334# ifdef PNG_SETJMP_SUPPORTED35/* We may have a jmp_buf left to deallocate. */36png_free_jmpbuf(&dummy_struct);37# endif38}39}4041/* Allocate memory. For reasonable files, size should never exceed42* 64K. However, zlib may allocate more than 64K if you don't tell43* it not to. See zconf.h and png.h for more information. zlib does44* need to allocate exactly 64K, so whatever you call here must45* have the ability to do that.46*/47PNG_FUNCTION(png_voidp,PNGAPI48png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)49{50png_voidp ret;5152ret = png_malloc(png_ptr, size);5354if (ret != NULL)55memset(ret, 0, size);5657return ret;58}5960/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of61* allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.62* Checking and error handling must happen outside this routine; it returns NULL63* if the allocation cannot be done (for any reason.)64*/65PNG_FUNCTION(png_voidp /* PRIVATE */,66png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),67PNG_ALLOCATED)68{69/* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS70* allocators have also been removed in 1.6.0, so any 16-bit system now has71* to implement a user memory handler. This checks to be sure it isn't72* called with big numbers.73*/74# ifdef PNG_MAX_MALLOC_64K75/* This is support for legacy systems which had segmented addressing76* limiting the maximum allocation size to 65536. It takes precedence77* over PNG_SIZE_MAX which is set to 65535 on true 16-bit systems.78*79* TODO: libpng-1.8: finally remove both cases.80*/81if (size > 65536U) return NULL;82# endif8384/* This is checked too because the system malloc call below takes a (size_t).85*/86if (size > PNG_SIZE_MAX) return NULL;8788# ifdef PNG_USER_MEM_SUPPORTED89if (png_ptr != NULL && png_ptr->malloc_fn != NULL)90return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);91# else92PNG_UNUSED(png_ptr)93# endif9495/* Use the system malloc */96return malloc((size_t)/*SAFE*/size); /* checked for truncation above */97}9899#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\100defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)101/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7102* that arises because of the checks in png_realloc_array that are repeated in103* png_malloc_array.104*/105static png_voidp106png_malloc_array_checked(png_const_structrp png_ptr, int nelements,107size_t element_size)108{109png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */110111if (req <= PNG_SIZE_MAX/element_size)112return png_malloc_base(png_ptr, req * element_size);113114/* The failure case when the request is too large */115return NULL;116}117118PNG_FUNCTION(png_voidp /* PRIVATE */,119png_malloc_array,(png_const_structrp png_ptr, int nelements,120size_t element_size),PNG_ALLOCATED)121{122if (nelements <= 0 || element_size == 0)123png_error(png_ptr, "internal error: array alloc");124125return png_malloc_array_checked(png_ptr, nelements, element_size);126}127128PNG_FUNCTION(png_voidp /* PRIVATE */,129png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,130int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)131{132/* These are internal errors: */133if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||134(old_array == NULL && old_elements > 0))135png_error(png_ptr, "internal error: array realloc");136137/* Check for overflow on the elements count (so the caller does not have to138* check.)139*/140if (add_elements <= INT_MAX - old_elements)141{142png_voidp new_array = png_malloc_array_checked(png_ptr,143old_elements+add_elements, element_size);144145if (new_array != NULL)146{147/* Because png_malloc_array worked the size calculations below cannot148* overflow.149*/150if (old_elements > 0)151memcpy(new_array, old_array, element_size*(unsigned)old_elements);152153memset((char*)new_array + element_size*(unsigned)old_elements, 0,154element_size*(unsigned)add_elements);155156return new_array;157}158}159160return NULL; /* error */161}162#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */163164/* Various functions that have different error handling are derived from this.165* png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate166* function png_malloc_default is also provided.167*/168PNG_FUNCTION(png_voidp,PNGAPI169png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)170{171png_voidp ret;172173if (png_ptr == NULL)174return NULL;175176ret = png_malloc_base(png_ptr, size);177178if (ret == NULL)179png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */180181return ret;182}183184#ifdef PNG_USER_MEM_SUPPORTED185PNG_FUNCTION(png_voidp,PNGAPI186png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),187PNG_ALLOCATED PNG_DEPRECATED)188{189png_voidp ret;190191if (png_ptr == NULL)192return NULL;193194/* Passing 'NULL' here bypasses the application provided memory handler. */195ret = png_malloc_base(NULL/*use malloc*/, size);196197if (ret == NULL)198png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */199200return ret;201}202#endif /* USER_MEM */203204/* This function was added at libpng version 1.2.3. The png_malloc_warn()205* function will issue a png_warning and return NULL instead of issuing a206* png_error, if it fails to allocate the requested memory.207*/208PNG_FUNCTION(png_voidp,PNGAPI209png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),210PNG_ALLOCATED)211{212if (png_ptr != NULL)213{214png_voidp ret = png_malloc_base(png_ptr, size);215216if (ret != NULL)217return ret;218219png_warning(png_ptr, "Out of memory");220}221222return NULL;223}224225/* Free a pointer allocated by png_malloc(). If ptr is NULL, return226* without taking any action.227*/228void PNGAPI229png_free(png_const_structrp png_ptr, png_voidp ptr)230{231if (png_ptr == NULL || ptr == NULL)232return;233234#ifdef PNG_USER_MEM_SUPPORTED235if (png_ptr->free_fn != NULL)236png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);237238else239png_free_default(png_ptr, ptr);240}241242PNG_FUNCTION(void,PNGAPI243png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)244{245if (png_ptr == NULL || ptr == NULL)246return;247#endif /* USER_MEM */248249free(ptr);250}251252#ifdef PNG_USER_MEM_SUPPORTED253/* This function is called when the application wants to use another method254* of allocating and freeing memory.255*/256void PNGAPI257png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr258malloc_fn, png_free_ptr free_fn)259{260if (png_ptr != NULL)261{262png_ptr->mem_ptr = mem_ptr;263png_ptr->malloc_fn = malloc_fn;264png_ptr->free_fn = free_fn;265}266}267268/* This function returns a pointer to the mem_ptr associated with the user269* functions. The application should free any memory associated with this270* pointer before png_write_destroy and png_read_destroy are called.271*/272png_voidp PNGAPI273png_get_mem_ptr(png_const_structrp png_ptr)274{275if (png_ptr == NULL)276return NULL;277278return png_ptr->mem_ptr;279}280#endif /* USER_MEM */281#endif /* READ || WRITE */282283284