remove some inlining directives
[platform/upstream/flac.git] / include / share / alloc.h
1 /* alloc - Convenience routines for safely allocating memory
2  * Copyright (C) 2007,2008  Josh Coalson
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #ifndef FLAC__SHARE__ALLOC_H
20 #define FLAC__SHARE__ALLOC_H
21
22 #if HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
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
28  */
29
30 #include <limits.h> /* for SIZE_MAX */
31 #if HAVE_STDINT_H
32 #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
33 #endif
34 #include <stdlib.h> /* for size_t, malloc(), etc */
35
36 #ifndef SIZE_MAX
37 # ifndef SIZE_T_MAX
38 #  ifdef _MSC_VER
39 #   define SIZE_T_MAX UINT_MAX
40 #  else
41 #   error
42 #  endif
43 # endif
44 # define SIZE_MAX SIZE_T_MAX
45 #endif
46
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
49 */
50 static void *safe_malloc_(size_t size)
51 {
52         /* malloc(0) is undefined; FLAC src convention is to always allocate */
53         if(!size)
54                 size++;
55         return malloc(size);
56 }
57
58 static void *safe_calloc_(size_t nmemb, size_t size)
59 {
60         if(!nmemb || !size)
61                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
62         return calloc(nmemb, size);
63 }
64
65 /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
66
67 static void *safe_malloc_add_2op_(size_t size1, size_t size2)
68 {
69         size2 += size1;
70         if(size2 < size1)
71                 return 0;
72         return safe_malloc_(size2);
73 }
74
75 static void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
76 {
77         size2 += size1;
78         if(size2 < size1)
79                 return 0;
80         size3 += size2;
81         if(size3 < size2)
82                 return 0;
83         return safe_malloc_(size3);
84 }
85
86 static void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
87 {
88         size2 += size1;
89         if(size2 < size1)
90                 return 0;
91         size3 += size2;
92         if(size3 < size2)
93                 return 0;
94         size4 += size3;
95         if(size4 < size3)
96                 return 0;
97         return safe_malloc_(size4);
98 }
99
100 static void *safe_malloc_mul_2op_(size_t size1, size_t size2)
101 #if 0
102 needs support for cases where sizeof(size_t) != 4
103 {
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);
108         }
109         return 0;
110 }
111 #else
112 /* better? */
113 {
114         if(!size1 || !size2)
115                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
116         if(size1 > SIZE_MAX / size2)
117                 return 0;
118         return malloc(size1*size2);
119 }
120 #endif
121
122 static void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
123 {
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)
127                 return 0;
128         size1 *= size2;
129         if(size1 > SIZE_MAX / size3)
130                 return 0;
131         return malloc(size1*size3);
132 }
133
134 /* size1*size2 + size3 */
135 static void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
136 {
137         if(!size1 || !size2)
138                 return safe_malloc_(size3);
139         if(size1 > SIZE_MAX / size2)
140                 return 0;
141         return safe_malloc_add_2op_(size1*size2, size3);
142 }
143
144 /* size1 * (size2 + size3) */
145 static void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
146 {
147         if(!size1 || (!size2 && !size3))
148                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
149         size2 += size3;
150         if(size2 < size3)
151                 return 0;
152         return safe_malloc_mul_2op_(size1, size2);
153 }
154
155 static void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
156 {
157         size2 += size1;
158         if(size2 < size1)
159                 return 0;
160         return realloc(ptr, size2);
161 }
162
163 static void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
164 {
165         size2 += size1;
166         if(size2 < size1)
167                 return 0;
168         size3 += size2;
169         if(size3 < size2)
170                 return 0;
171         return realloc(ptr, size3);
172 }
173
174 static void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
175 {
176         size2 += size1;
177         if(size2 < size1)
178                 return 0;
179         size3 += size2;
180         if(size3 < size2)
181                 return 0;
182         size4 += size3;
183         if(size4 < size3)
184                 return 0;
185         return realloc(ptr, size4);
186 }
187
188 static void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
189 {
190         if(!size1 || !size2)
191                 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
192         if(size1 > SIZE_MAX / size2)
193                 return 0;
194         return realloc(ptr, size1*size2);
195 }
196
197 /* size1 * (size2 + size3) */
198 static void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
199 {
200         if(!size1 || (!size2 && !size3))
201                 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
202         size2 += size3;
203         if(size2 < size3)
204                 return 0;
205         return safe_realloc_mul_2op_(ptr, size1, size2);
206 }
207
208 #endif