pngmem.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* pngmem.c - stub functions for memory allocation
  2. *
  3. * Copyright (c) 2018-2025 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2014,2016 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 memory allocation. Users who
  13. * need special memory handling are expected to supply replacement
  14. * functions for png_malloc() and png_free(), and to use
  15. * png_create_read_struct_2() and png_create_write_struct_2() to
  16. * identify the replacement functions.
  17. */
  18. #include "pngpriv.h"
  19. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  20. /* Free a png_struct */
  21. void /* PRIVATE */
  22. png_destroy_png_struct(png_structrp png_ptr)
  23. {
  24. if (png_ptr != NULL)
  25. {
  26. /* png_free might call png_error and may certainly call
  27. * png_get_mem_ptr, so fake a temporary png_struct to support this.
  28. */
  29. png_struct dummy_struct = *png_ptr;
  30. memset(png_ptr, 0, (sizeof *png_ptr));
  31. png_free(&dummy_struct, png_ptr);
  32. # ifdef PNG_SETJMP_SUPPORTED
  33. /* We may have a jmp_buf left to deallocate. */
  34. png_free_jmpbuf(&dummy_struct);
  35. # endif
  36. }
  37. }
  38. /* Allocate memory. For reasonable files, size should never exceed
  39. * 64K. However, zlib may allocate more than 64K if you don't tell
  40. * it not to. See zconf.h and png.h for more information. zlib does
  41. * need to allocate exactly 64K, so whatever you call here must
  42. * have the ability to do that.
  43. */
  44. PNG_FUNCTION(png_voidp,PNGAPI
  45. png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
  46. {
  47. png_voidp ret;
  48. ret = png_malloc(png_ptr, size);
  49. if (ret != NULL)
  50. memset(ret, 0, size);
  51. return ret;
  52. }
  53. /* png_malloc_base, an internal function added at libpng 1.6.0, does the work of
  54. * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
  55. * Checking and error handling must happen outside this routine; it returns NULL
  56. * if the allocation cannot be done (for any reason.)
  57. */
  58. PNG_FUNCTION(png_voidp /* PRIVATE */,
  59. png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
  60. PNG_ALLOCATED)
  61. {
  62. /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
  63. * allocators have also been removed in 1.6.0, so any 16-bit system now has
  64. * to implement a user memory handler. This checks to be sure it isn't
  65. * called with big numbers.
  66. */
  67. # ifdef PNG_MAX_MALLOC_64K
  68. /* This is support for legacy systems which had segmented addressing
  69. * limiting the maximum allocation size to 65536. It takes precedence
  70. * over PNG_SIZE_MAX which is set to 65535 on true 16-bit systems.
  71. *
  72. * TODO: libpng-1.8: finally remove both cases.
  73. */
  74. if (size > 65536U) return NULL;
  75. # endif
  76. /* This is checked too because the system malloc call below takes a (size_t).
  77. */
  78. if (size > PNG_SIZE_MAX) return NULL;
  79. # ifdef PNG_USER_MEM_SUPPORTED
  80. if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
  81. return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
  82. # else
  83. PNG_UNUSED(png_ptr)
  84. # endif
  85. /* Use the system malloc */
  86. return malloc((size_t)/*SAFE*/size); /* checked for truncation above */
  87. }
  88. #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
  89. defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
  90. /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
  91. * that arises because of the checks in png_realloc_array that are repeated in
  92. * png_malloc_array.
  93. */
  94. static png_voidp
  95. png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
  96. size_t element_size)
  97. {
  98. png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */
  99. if (req <= PNG_SIZE_MAX/element_size)
  100. return png_malloc_base(png_ptr, req * element_size);
  101. /* The failure case when the request is too large */
  102. return NULL;
  103. }
  104. PNG_FUNCTION(png_voidp /* PRIVATE */,
  105. png_malloc_array,(png_const_structrp png_ptr, int nelements,
  106. size_t element_size),PNG_ALLOCATED)
  107. {
  108. if (nelements <= 0 || element_size == 0)
  109. png_error(png_ptr, "internal error: array alloc");
  110. return png_malloc_array_checked(png_ptr, nelements, element_size);
  111. }
  112. PNG_FUNCTION(png_voidp /* PRIVATE */,
  113. png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
  114. int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
  115. {
  116. /* These are internal errors: */
  117. if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
  118. (old_array == NULL && old_elements > 0))
  119. png_error(png_ptr, "internal error: array realloc");
  120. /* Check for overflow on the elements count (so the caller does not have to
  121. * check.)
  122. */
  123. if (add_elements <= INT_MAX - old_elements)
  124. {
  125. png_voidp new_array = png_malloc_array_checked(png_ptr,
  126. old_elements+add_elements, element_size);
  127. if (new_array != NULL)
  128. {
  129. /* Because png_malloc_array worked the size calculations below cannot
  130. * overflow.
  131. */
  132. if (old_elements > 0)
  133. memcpy(new_array, old_array, element_size*(unsigned)old_elements);
  134. memset((char*)new_array + element_size*(unsigned)old_elements, 0,
  135. element_size*(unsigned)add_elements);
  136. return new_array;
  137. }
  138. }
  139. return NULL; /* error */
  140. }
  141. #endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */
  142. /* Various functions that have different error handling are derived from this.
  143. * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
  144. * function png_malloc_default is also provided.
  145. */
  146. PNG_FUNCTION(png_voidp,PNGAPI
  147. png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
  148. {
  149. png_voidp ret;
  150. if (png_ptr == NULL)
  151. return NULL;
  152. ret = png_malloc_base(png_ptr, size);
  153. if (ret == NULL)
  154. png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
  155. return ret;
  156. }
  157. #ifdef PNG_USER_MEM_SUPPORTED
  158. PNG_FUNCTION(png_voidp,PNGAPI
  159. png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
  160. PNG_ALLOCATED PNG_DEPRECATED)
  161. {
  162. png_voidp ret;
  163. if (png_ptr == NULL)
  164. return NULL;
  165. /* Passing 'NULL' here bypasses the application provided memory handler. */
  166. ret = png_malloc_base(NULL/*use malloc*/, size);
  167. if (ret == NULL)
  168. png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
  169. return ret;
  170. }
  171. #endif /* USER_MEM */
  172. /* This function was added at libpng version 1.2.3. The png_malloc_warn()
  173. * function will issue a png_warning and return NULL instead of issuing a
  174. * png_error, if it fails to allocate the requested memory.
  175. */
  176. PNG_FUNCTION(png_voidp,PNGAPI
  177. png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
  178. PNG_ALLOCATED)
  179. {
  180. if (png_ptr != NULL)
  181. {
  182. png_voidp ret = png_malloc_base(png_ptr, size);
  183. if (ret != NULL)
  184. return ret;
  185. png_warning(png_ptr, "Out of memory");
  186. }
  187. return NULL;
  188. }
  189. /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
  190. * without taking any action.
  191. */
  192. void PNGAPI
  193. png_free(png_const_structrp png_ptr, png_voidp ptr)
  194. {
  195. if (png_ptr == NULL || ptr == NULL)
  196. return;
  197. #ifdef PNG_USER_MEM_SUPPORTED
  198. if (png_ptr->free_fn != NULL)
  199. png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
  200. else
  201. png_free_default(png_ptr, ptr);
  202. }
  203. PNG_FUNCTION(void,PNGAPI
  204. png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
  205. {
  206. if (png_ptr == NULL || ptr == NULL)
  207. return;
  208. #endif /* USER_MEM */
  209. free(ptr);
  210. }
  211. #ifdef PNG_USER_MEM_SUPPORTED
  212. /* This function is called when the application wants to use another method
  213. * of allocating and freeing memory.
  214. */
  215. void PNGAPI
  216. png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  217. malloc_fn, png_free_ptr free_fn)
  218. {
  219. if (png_ptr != NULL)
  220. {
  221. png_ptr->mem_ptr = mem_ptr;
  222. png_ptr->malloc_fn = malloc_fn;
  223. png_ptr->free_fn = free_fn;
  224. }
  225. }
  226. /* This function returns a pointer to the mem_ptr associated with the user
  227. * functions. The application should free any memory associated with this
  228. * pointer before png_write_destroy and png_read_destroy are called.
  229. */
  230. png_voidp PNGAPI
  231. png_get_mem_ptr(png_const_structrp png_ptr)
  232. {
  233. if (png_ptr == NULL)
  234. return NULL;
  235. return png_ptr->mem_ptr;
  236. }
  237. #endif /* USER_MEM */
  238. #endif /* READ || WRITE */