1 /* alloc - Convenience routines for safely allocating memory
2 * Copyright (C) 2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef FLAC__SHARE__ALLOC_H
33 #define FLAC__SHARE__ALLOC_H
39 /* WATCHOUT: for c++ you may have to #define __STDC_LIMIT_MACROS 1 real early
40 * before #including this file, otherwise SIZE_MAX might not be defined
43 #include <limits.h> /* for SIZE_MAX */
45 #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
47 #include <stdlib.h> /* for size_t, malloc(), etc */
52 # define SIZE_T_MAX UINT_MAX
57 # define SIZE_MAX SIZE_T_MAX
60 /* avoid malloc()ing 0 bytes, see:
61 * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
63 static inline void *safe_malloc_(size_t size)
65 /* malloc(0) is undefined; FLAC src convention is to always allocate */
71 static inline void *safe_calloc_(size_t nmemb, size_t size)
74 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
75 return calloc(nmemb, size);
78 /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
80 static inline void *safe_malloc_add_2op_(size_t size1, size_t size2)
85 return safe_malloc_(size2);
88 static inline void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
96 return safe_malloc_(size3);
99 static inline void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
110 return safe_malloc_(size4);
113 static inline void *safe_malloc_mul_2op_(size_t size1, size_t size2)
115 needs support for cases where sizeof(size_t) != 4
117 /* could be faster #ifdef'ing off SIZEOF_SIZE_T */
118 if(sizeof(size_t) == 4) {
119 if ((double)size1 * (double)size2 < 4294967296.0)
120 return malloc(size1*size2);
128 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
129 if(size1 > SIZE_MAX / size2)
131 return malloc(size1*size2);
135 static inline void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
137 if(!size1 || !size2 || !size3)
138 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
139 if(size1 > SIZE_MAX / size2)
142 if(size1 > SIZE_MAX / size3)
144 return malloc(size1*size3);
147 /* size1*size2 + size3 */
148 static inline void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
151 return safe_malloc_(size3);
152 if(size1 > SIZE_MAX / size2)
154 return safe_malloc_add_2op_(size1*size2, size3);
157 /* size1 * (size2 + size3) */
158 static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
160 if(!size1 || (!size2 && !size3))
161 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
165 return safe_malloc_mul_2op_(size1, size2);
168 static inline void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
173 return realloc(ptr, size2);
176 static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
184 return realloc(ptr, size3);
187 static inline void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
198 return realloc(ptr, size4);
201 static inline void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
204 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
205 if(size1 > SIZE_MAX / size2)
207 return realloc(ptr, size1*size2);
210 /* size1 * (size2 + size3) */
211 static inline void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
213 if(!size1 || (!size2 && !size3))
214 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
218 return safe_realloc_mul_2op_(ptr, size1, size2);