1 /* alloc - Convenience routines for safely allocating memory
2 * Copyright (C) 2007 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef FLAC__SHARE__ALLOC_H
20 #define FLAC__SHARE__ALLOC_H
26 /* WATCHOUT: for c++ you may have to #define __STDC_LIMIT_MACROS 1 real early
27 * before #including this file, otherwise SIZE_MAX might not be defined
30 #include <limits.h> /* for SIZE_MAX */
31 #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
32 #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
34 #include <stdlib.h> /* for size_t, malloc(), etc */
39 # define SIZE_T_MAX UINT_MAX
44 # define SIZE_MAX SIZE_T_MAX
51 /* avoid malloc()ing 0 bytes, see:
52 * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
54 static FLaC__INLINE void *safe_malloc_(size_t size)
56 /* malloc(0) is undefined; FLAC src convention is to always allocate */
62 static FLaC__INLINE void *safe_calloc_(size_t nmemb, size_t size)
65 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
66 return calloc(nmemb, size);
69 /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
71 static FLaC__INLINE void *safe_malloc_add_2op_(size_t size1, size_t size2)
76 return safe_malloc_(size2);
79 static FLaC__INLINE void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
87 return safe_malloc_(size3);
90 static FLaC__INLINE void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
101 return safe_malloc_(size4);
104 static FLaC__INLINE void *safe_malloc_mul_2op_(size_t size1, size_t size2)
106 needs support for cases where sizeof(size_t) != 4
108 /* could be faster #ifdef'ing off SIZEOF_SIZE_T */
109 if(sizeof(size_t) == 4) {
110 if ((double)size1 * (double)size2 < 4294967296.0)
111 return malloc(size1*size2);
119 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
120 if(size1 > SIZE_MAX / size2)
122 return malloc(size1*size2);
126 static FLaC__INLINE void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
128 if(!size1 || !size2 || !size3)
129 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
130 if(size1 > SIZE_MAX / size2)
133 if(size1 > SIZE_MAX / size3)
135 return malloc(size1*size3);
138 /* size1*size2 + size3 */
139 static FLaC__INLINE void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
142 return safe_malloc_(size3);
143 if(size1 > SIZE_MAX / size2)
145 return safe_malloc_add_2op_(size1*size2, size3);
148 /* size1 * (size2 + size3) */
149 static FLaC__INLINE void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
151 if(!size1 || (!size2 && !size3))
152 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
156 return safe_malloc_mul_2op_(size1, size2);
159 static FLaC__INLINE void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
164 return realloc(ptr, size2);
167 static FLaC__INLINE void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
175 return realloc(ptr, size3);
178 static FLaC__INLINE void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
189 return realloc(ptr, size4);
192 static FLaC__INLINE void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
195 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
196 if(size1 > SIZE_MAX / size2)
198 return realloc(ptr, size1*size2);
201 /* size1 * (size2 + size3) */
202 static FLaC__INLINE void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
204 if(!size1 || (!size2 && !size3))
205 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
209 return safe_realloc_mul_2op_(ptr, size1, size2);