media/libpng/pngmem.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial