8fc17f7652e83a766fc71e3912ddae32d0b5f2df
[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 #include "share/compat.h"
49
50 #ifndef SIZE_MAX
51 # ifndef SIZE_T_MAX
52 #  ifdef _MSC_VER
53 #   define SIZE_T_MAX SIZE_MAX
54 #  else
55 #   error
56 #  endif
57 # endif
58 # define SIZE_MAX SIZE_T_MAX
59 #endif
60
61 /* avoid malloc()ing 0 bytes, see:
62  * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
63 */
64 static inline void *safe_malloc_(size_t size)
65 {
66         /* malloc(0) is undefined; FLAC src convention is to always allocate */
67         if(!size)
68                 size++;
69         return malloc(size);
70 }
71
72 static inline void *safe_calloc_(size_t nmemb, size_t size)
73 {
74         if(!nmemb || !size)
75                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
76         return calloc(nmemb, size);
77 }
78
79 /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
80
81 static inline void *safe_malloc_add_2op_(size_t size1, size_t size2)
82 {
83         size2 += size1;
84         if(size2 < size1)
85                 return 0;
86         return safe_malloc_(size2);
87 }
88
89 static inline void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
90 {
91         size2 += size1;
92         if(size2 < size1)
93                 return 0;
94         size3 += size2;
95         if(size3 < size2)
96                 return 0;
97         return safe_malloc_(size3);
98 }
99
100 static inline void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
101 {
102         size2 += size1;
103         if(size2 < size1)
104                 return 0;
105         size3 += size2;
106         if(size3 < size2)
107                 return 0;
108         size4 += size3;
109         if(size4 < size3)
110                 return 0;
111         return safe_malloc_(size4);
112 }
113
114 void *safe_malloc_mul_2op_(size_t size1, size_t size2) ;
115
116 static inline void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
117 {
118         if(!size1 || !size2 || !size3)
119                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
120         if(size1 > SIZE_MAX / size2)
121                 return 0;
122         size1 *= size2;
123         if(size1 > SIZE_MAX / size3)
124                 return 0;
125         return malloc(size1*size3);
126 }
127
128 /* size1*size2 + size3 */
129 static inline void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
130 {
131         if(!size1 || !size2)
132                 return safe_malloc_(size3);
133         if(size1 > SIZE_MAX / size2)
134                 return 0;
135         return safe_malloc_add_2op_(size1*size2, size3);
136 }
137
138 /* size1 * (size2 + size3) */
139 static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
140 {
141         if(!size1 || (!size2 && !size3))
142                 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
143         size2 += size3;
144         if(size2 < size3)
145                 return 0;
146         if(size1 > SIZE_MAX / size2)
147                 return 0;
148         return malloc(size1*size2);
149 }
150
151 static inline void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
152 {
153         size2 += size1;
154         if(size2 < size1)
155                 return 0;
156         return realloc(ptr, size2);
157 }
158
159 static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
160 {
161         size2 += size1;
162         if(size2 < size1)
163                 return 0;
164         size3 += size2;
165         if(size3 < size2)
166                 return 0;
167         return realloc(ptr, size3);
168 }
169
170 static inline void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
171 {
172         size2 += size1;
173         if(size2 < size1)
174                 return 0;
175         size3 += size2;
176         if(size3 < size2)
177                 return 0;
178         size4 += size3;
179         if(size4 < size3)
180                 return 0;
181         return realloc(ptr, size4);
182 }
183
184 static inline void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
185 {
186         if(!size1 || !size2)
187                 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
188         if(size1 > SIZE_MAX / size2)
189                 return 0;
190         return realloc(ptr, size1*size2);
191 }
192
193 /* size1 * (size2 + size3) */
194 static inline void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
195 {
196         if(!size1 || (!size2 && !size3))
197                 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
198         size2 += size3;
199         if(size2 < size3)
200                 return 0;
201         return safe_realloc_mul_2op_(ptr, size1, size2);
202 }
203
204 #endif