8a53bbe5aa63a8027b6ffca9d33b786d3b039145
[platform/upstream/flac.git] / include / share / alloc.h
1 /* alloc - Convenience routines for safely allocating memory
2  * Copyright (C) 2007,2008,2009  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
30  */
31
32 #ifndef FLAC__SHARE__ALLOC_H
33 #define FLAC__SHARE__ALLOC_H
34
35 #if HAVE_CONFIG_H
36 #  include <config.h>
37 #endif
38
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
41  */
42
43 #include <limits.h> /* for SIZE_MAX */
44 #if HAVE_STDINT_H
45 #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
46 #endif
47 #include <stdlib.h> /* for size_t, malloc(), etc */
48
49 #ifndef SIZE_MAX
50 # ifndef SIZE_T_MAX
51 #  ifdef _MSC_VER
52 #   define SIZE_T_MAX UINT_MAX /* What happens on 64 bit windows? */
53 #  else
54 #   error
55 #  endif
56 # endif
57 # define SIZE_MAX SIZE_T_MAX
58 #endif
59
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
62 */
63 static inline void *safe_malloc_(size_t size)
64 {
65         /* malloc(0) is undefined; FLAC src convention is to always allocate */
66         if(!size)
67                 size++;
68         return malloc(size);
69 }
70
71 static inline void *safe_calloc_(size_t nmemb, size_t size)
72 {
73         if(!nmemb || !size)
74                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
75         return calloc(nmemb, size);
76 }
77
78 /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
79
80 static inline void *safe_malloc_add_2op_(size_t size1, size_t size2)
81 {
82         size2 += size1;
83         if(size2 < size1)
84                 return 0;
85         return safe_malloc_(size2);
86 }
87
88 static inline void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
89 {
90         size2 += size1;
91         if(size2 < size1)
92                 return 0;
93         size3 += size2;
94         if(size3 < size2)
95                 return 0;
96         return safe_malloc_(size3);
97 }
98
99 static inline void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
100 {
101         size2 += size1;
102         if(size2 < size1)
103                 return 0;
104         size3 += size2;
105         if(size3 < size2)
106                 return 0;
107         size4 += size3;
108         if(size4 < size3)
109                 return 0;
110         return safe_malloc_(size4);
111 }
112
113 void *safe_malloc_mul_2op_(size_t size1, size_t size2) ;
114
115 static inline void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
116 {
117         if(!size1 || !size2 || !size3)
118                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
119         if(size1 > SIZE_MAX / size2)
120                 return 0;
121         size1 *= size2;
122         if(size1 > SIZE_MAX / size3)
123                 return 0;
124         return malloc(size1*size3);
125 }
126
127 /* size1*size2 + size3 */
128 static inline void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
129 {
130         if(!size1 || !size2)
131                 return safe_malloc_(size3);
132         if(size1 > SIZE_MAX / size2)
133                 return 0;
134         return safe_malloc_add_2op_(size1*size2, size3);
135 }
136
137 /* size1 * (size2 + size3) */
138 static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
139 {
140         if(!size1 || (!size2 && !size3))
141                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
142         size2 += size3;
143         if(size2 < size3)
144                 return 0;
145         return safe_malloc_mul_2op_(size1, size2);
146 }
147
148 static inline void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
149 {
150         size2 += size1;
151         if(size2 < size1)
152                 return 0;
153         return realloc(ptr, size2);
154 }
155
156 static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
157 {
158         size2 += size1;
159         if(size2 < size1)
160                 return 0;
161         size3 += size2;
162         if(size3 < size2)
163                 return 0;
164         return realloc(ptr, size3);
165 }
166
167 static inline void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
168 {
169         size2 += size1;
170         if(size2 < size1)
171                 return 0;
172         size3 += size2;
173         if(size3 < size2)
174                 return 0;
175         size4 += size3;
176         if(size4 < size3)
177                 return 0;
178         return realloc(ptr, size4);
179 }
180
181 static inline void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
182 {
183         if(!size1 || !size2)
184                 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
185         if(size1 > SIZE_MAX / size2)
186                 return 0;
187         return realloc(ptr, size1*size2);
188 }
189
190 /* size1 * (size2 + size3) */
191 static inline void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
192 {
193         if(!size1 || (!size2 && !size3))
194                 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
195         size2 += size3;
196         if(size2 < size3)
197                 return 0;
198         return safe_realloc_mul_2op_(ptr, size1, size2);
199 }
200
201 #endif