pngmem.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* pngmem.c - stub functions for memory allocation
  2. *
  3. * Copyright (c) 2018 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. #ifndef PNG_USER_MEM_SUPPORTED
  68. PNG_UNUSED(png_ptr)
  69. #endif
  70. /* Some compilers complain that this is always true. However, it
  71. * can be false when integer overflow happens.
  72. */
  73. if (size > 0 && size <= PNG_SIZE_MAX
  74. # ifdef PNG_MAX_MALLOC_64K
  75. && size <= 65536U
  76. # endif
  77. )
  78. {
  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. #endif
  84. return malloc((size_t)size); /* checked for truncation above */
  85. }
  86. else
  87. return NULL;
  88. }
  89. #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
  90. defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
  91. /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
  92. * that arises because of the checks in png_realloc_array that are repeated in
  93. * png_malloc_array.
  94. */
  95. static png_voidp
  96. png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
  97. size_t element_size)
  98. {
  99. png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */
  100. if (req <= PNG_SIZE_MAX/element_size)
  101. return png_malloc_base(png_ptr, req * element_size);
  102. /* The failure case when the request is too large */
  103. return NULL;
  104. }
  105. PNG_FUNCTION(png_voidp /* PRIVATE */,
  106. png_malloc_array,(png_const_structrp png_ptr, int nelements,
  107. size_t element_size),PNG_ALLOCATED)
  108. {
  109. if (nelements <= 0 || element_size == 0)
  110. png_error(png_ptr, "internal error: array alloc");
  111. return png_malloc_array_checked(png_ptr, nelements, element_size);
  112. }
  113. PNG_FUNCTION(png_voidp /* PRIVATE */,
  114. png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
  115. int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
  116. {
  117. /* These are internal errors: */
  118. if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
  119. (old_array == NULL && old_elements > 0))
  120. png_error(png_ptr, "internal error: array realloc");
  121. /* Check for overflow on the elements count (so the caller does not have to
  122. * check.)
  123. */
  124. if (add_elements <= INT_MAX - old_elements)
  125. {
  126. png_voidp new_array = png_malloc_array_checked(png_ptr,
  127. old_elements+add_elements, element_size);
  128. if (new_array != NULL)
  129. {
  130. /* Because png_malloc_array worked the size calculations below cannot
  131. * overflow.
  132. */
  133. if (old_elements > 0)
  134. memcpy(new_array, old_array, element_size*(unsigned)old_elements);
  135. memset((char*)new_array + element_size*(unsigned)old_elements, 0,
  136. element_size*(unsigned)add_elements);
  137. return new_array;
  138. }
  139. }
  140. return NULL; /* error */
  141. }
  142. #endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */
  143. /* Various functions that have different error handling are derived from this.
  144. * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
  145. * function png_malloc_default is also provided.
  146. */
  147. PNG_FUNCTION(png_voidp,PNGAPI
  148. png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
  149. {
  150. png_voidp ret;
  151. if (png_ptr == NULL)
  152. return NULL;
  153. ret = png_malloc_base(png_ptr, size);
  154. if (ret == NULL)
  155. png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
  156. return ret;
  157. }
  158. #ifdef PNG_USER_MEM_SUPPORTED
  159. PNG_FUNCTION(png_voidp,PNGAPI
  160. png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
  161. PNG_ALLOCATED PNG_DEPRECATED)
  162. {
  163. png_voidp ret;
  164. if (png_ptr == NULL)
  165. return NULL;
  166. /* Passing 'NULL' here bypasses the application provided memory handler. */
  167. ret = png_malloc_base(NULL/*use malloc*/, size);
  168. if (ret == NULL)
  169. png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
  170. return ret;
  171. }
  172. #endif /* USER_MEM */
  173. /* This function was added at libpng version 1.2.3. The png_malloc_warn()
  174. * function will issue a png_warning and return NULL instead of issuing a
  175. * png_error, if it fails to allocate the requested memory.
  176. */
  177. PNG_FUNCTION(png_voidp,PNGAPI
  178. png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
  179. PNG_ALLOCATED)
  180. {
  181. if (png_ptr != NULL)
  182. {
  183. png_voidp ret = png_malloc_base(png_ptr, size);
  184. if (ret != NULL)
  185. return ret;
  186. png_warning(png_ptr, "Out of memory");
  187. }
  188. return NULL;
  189. }
  190. /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
  191. * without taking any action.
  192. */
  193. void PNGAPI
  194. png_free(png_const_structrp png_ptr, png_voidp ptr)
  195. {
  196. if (png_ptr == NULL || ptr == NULL)
  197. return;
  198. #ifdef PNG_USER_MEM_SUPPORTED
  199. if (png_ptr->free_fn != NULL)
  200. png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
  201. else
  202. png_free_default(png_ptr, ptr);
  203. }
  204. PNG_FUNCTION(void,PNGAPI
  205. png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
  206. {
  207. if (png_ptr == NULL || ptr == NULL)
  208. return;
  209. #endif /* USER_MEM */
  210. free(ptr);
  211. }
  212. #ifdef PNG_USER_MEM_SUPPORTED
  213. /* This function is called when the application wants to use another method
  214. * of allocating and freeing memory.
  215. */
  216. void PNGAPI
  217. png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  218. malloc_fn, png_free_ptr free_fn)
  219. {
  220. if (png_ptr != NULL)
  221. {
  222. png_ptr->mem_ptr = mem_ptr;
  223. png_ptr->malloc_fn = malloc_fn;
  224. png_ptr->free_fn = free_fn;
  225. }
  226. }
  227. /* This function returns a pointer to the mem_ptr associated with the user
  228. * functions. The application should free any memory associated with this
  229. * pointer before png_write_destroy and png_read_destroy are called.
  230. */
  231. png_voidp PNGAPI
  232. png_get_mem_ptr(png_const_structrp png_ptr)
  233. {
  234. if (png_ptr == NULL)
  235. return NULL;
  236. return png_ptr->mem_ptr;
  237. }
  238. #endif /* USER_MEM */
  239. #endif /* READ || WRITE */