1 /* alloc - Convenience routines for safely allocating memory
2 * Copyright (C) 2007,2008 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 */
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
47 /* avoid malloc()ing 0 bytes, see:
48 * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
50 static void *safe_malloc_(size_t size)
52 /* malloc(0) is undefined; FLAC src convention is to always allocate */
58 static void *safe_calloc_(size_t nmemb, size_t size)
61 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
62 return calloc(nmemb, size);
65 /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
67 static void *safe_malloc_add_2op_(size_t size1, size_t size2)
72 return safe_malloc_(size2);
75 static void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
83 return safe_malloc_(size3);
86 static void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
97 return safe_malloc_(size4);
100 static void *safe_malloc_mul_2op_(size_t size1, size_t size2)
102 needs support for cases where sizeof(size_t) != 4
104 /* could be faster #ifdef'ing off SIZEOF_SIZE_T */
105 if(sizeof(size_t) == 4) {
106 if ((double)size1 * (double)size2 < 4294967296.0)
107 return malloc(size1*size2);
115 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
116 if(size1 > SIZE_MAX / size2)
118 return malloc(size1*size2);
122 static void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
124 if(!size1 || !size2 || !size3)
125 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
126 if(size1 > SIZE_MAX / size2)
129 if(size1 > SIZE_MAX / size3)
131 return malloc(size1*size3);
134 /* size1*size2 + size3 */
135 static void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
138 return safe_malloc_(size3);
139 if(size1 > SIZE_MAX / size2)
141 return safe_malloc_add_2op_(size1*size2, size3);
144 /* size1 * (size2 + size3) */
145 static void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
147 if(!size1 || (!size2 && !size3))
148 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
152 return safe_malloc_mul_2op_(size1, size2);
155 static void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
160 return realloc(ptr, size2);
163 static void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
171 return realloc(ptr, size3);
174 static void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
185 return realloc(ptr, size4);
188 static void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
191 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
192 if(size1 > SIZE_MAX / size2)
194 return realloc(ptr, size1*size2);
197 /* size1 * (size2 + size3) */
198 static void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
200 if(!size1 || (!size2 && !size3))
201 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
205 return safe_realloc_mul_2op_(ptr, size1, size2);