/* 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),49PNG_ALLOCATED)50{51png_voidp ret;5253ret = png_malloc(png_ptr, size);5455if (ret != NULL)56memset(ret, 0, size);5758return ret;59}6061/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of62* allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.63* Checking and error handling must happen outside this routine; it returns NULL64* if the allocation cannot be done (for any reason.)65*/66PNG_FUNCTION(png_voidp /* PRIVATE */,67png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),68PNG_ALLOCATED)69{70/* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS71* allocators have also been removed in 1.6.0, so any 16-bit system now has72* to implement a user memory handler. This checks to be sure it isn't73* called with big numbers.74*/75# ifdef PNG_MAX_MALLOC_64K76/* This is support for legacy systems which had segmented addressing77* limiting the maximum allocation size to 65536. It takes precedence78* over PNG_SIZE_MAX which is set to 65535 on true 16-bit systems.79*80* TODO: libpng-1.8: finally remove both cases.81*/82if (size > 65536U) return NULL;83# endif8485/* This is checked too because the system malloc call below takes a (size_t).86*/87if (size > PNG_SIZE_MAX) return NULL;8889# ifdef PNG_USER_MEM_SUPPORTED90if (png_ptr != NULL && png_ptr->malloc_fn != NULL)91return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);92# else93PNG_UNUSED(png_ptr)94# endif9596/* Use the system malloc */97return malloc((size_t)/*SAFE*/size); /* checked for truncation above */98}99100#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\101defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)102/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7103* that arises because of the checks in png_realloc_array that are repeated in104* png_malloc_array.105*/106static png_voidp107png_malloc_array_checked(png_const_structrp png_ptr, int nelements,108size_t element_size)109{110png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */111112if (req <= PNG_SIZE_MAX/element_size)113return png_malloc_base(png_ptr, req * element_size);114115/* The failure case when the request is too large */116return NULL;117}118119PNG_FUNCTION(png_voidp /* PRIVATE */,120png_malloc_array,(png_const_structrp png_ptr, int nelements,121size_t element_size),122PNG_ALLOCATED)123{124if (nelements <= 0 || element_size == 0)125png_error(png_ptr, "internal error: array alloc");126127return png_malloc_array_checked(png_ptr, nelements, element_size);128}129130PNG_FUNCTION(png_voidp /* PRIVATE */,131png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,132int old_elements, int add_elements, size_t element_size),133PNG_ALLOCATED)134{135/* These are internal errors: */136if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||137(old_array == NULL && old_elements > 0))138png_error(png_ptr, "internal error: array realloc");139140/* Check for overflow on the elements count (so the caller does not have to141* check.)142*/143if (add_elements <= INT_MAX - old_elements)144{145png_voidp new_array = png_malloc_array_checked(png_ptr,146old_elements+add_elements, element_size);147148if (new_array != NULL)149{150/* Because png_malloc_array worked the size calculations below cannot151* overflow.152*/153if (old_elements > 0)154memcpy(new_array, old_array, element_size*(unsigned)old_elements);155156memset((char*)new_array + element_size*(unsigned)old_elements, 0,157element_size*(unsigned)add_elements);158159return new_array;160}161}162163return NULL; /* error */164}165#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */166167/* Various functions that have different error handling are derived from this.168* png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate169* function png_malloc_default is also provided.170*/171PNG_FUNCTION(png_voidp,PNGAPI172png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),173PNG_ALLOCATED)174{175png_voidp ret;176177if (png_ptr == NULL)178return NULL;179180ret = png_malloc_base(png_ptr, size);181182if (ret == NULL)183png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */184185return ret;186}187188#ifdef PNG_USER_MEM_SUPPORTED189PNG_FUNCTION(png_voidp,PNGAPI190png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),191PNG_ALLOCATED PNG_DEPRECATED)192{193png_voidp ret;194195if (png_ptr == NULL)196return NULL;197198/* Passing 'NULL' here bypasses the application provided memory handler. */199ret = png_malloc_base(NULL/*use malloc*/, size);200201if (ret == NULL)202png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */203204return ret;205}206#endif /* USER_MEM */207208/* This function was added at libpng version 1.2.3. The png_malloc_warn()209* function will issue a png_warning and return NULL instead of issuing a210* png_error, if it fails to allocate the requested memory.211*/212PNG_FUNCTION(png_voidp,PNGAPI213png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),214PNG_ALLOCATED)215{216if (png_ptr != NULL)217{218png_voidp ret = png_malloc_base(png_ptr, size);219220if (ret != NULL)221return ret;222223png_warning(png_ptr, "Out of memory");224}225226return NULL;227}228229/* Free a pointer allocated by png_malloc(). If ptr is NULL, return230* without taking any action.231*/232void PNGAPI233png_free(png_const_structrp png_ptr, png_voidp ptr)234{235if (png_ptr == NULL || ptr == NULL)236return;237238#ifdef PNG_USER_MEM_SUPPORTED239if (png_ptr->free_fn != NULL)240png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);241242else243png_free_default(png_ptr, ptr);244}245246PNG_FUNCTION(void,PNGAPI247png_free_default,(png_const_structrp png_ptr, png_voidp ptr),248PNG_DEPRECATED)249{250if (png_ptr == NULL || ptr == NULL)251return;252#endif /* USER_MEM */253254free(ptr);255}256257#ifdef PNG_USER_MEM_SUPPORTED258/* This function is called when the application wants to use another method259* of allocating and freeing memory.260*/261void PNGAPI262png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr,263png_malloc_ptr malloc_fn, png_free_ptr free_fn)264{265if (png_ptr != NULL)266{267png_ptr->mem_ptr = mem_ptr;268png_ptr->malloc_fn = malloc_fn;269png_ptr->free_fn = free_fn;270}271}272273/* This function returns a pointer to the mem_ptr associated with the user274* functions. The application should free any memory associated with this275* pointer before png_write_destroy and png_read_destroy are called.276*/277png_voidp PNGAPI278png_get_mem_ptr(png_const_structrp png_ptr)279{280if (png_ptr == NULL)281return NULL;282283return png_ptr->mem_ptr;284}285#endif /* USER_MEM */286#endif /* READ || WRITE */287288289