RetroArch
alloc.h
Go to the documentation of this file.
1 /* alloc - Convenience routines for safely allocating memory
2  * Copyright (C) 2007-2009 Josh Coalson
3  * Copyright (C) 2011-2016 Xiph.Org Foundation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Xiph.org Foundation nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef FLAC__SHARE__ALLOC_H
34 #define FLAC__SHARE__ALLOC_H
35 
36 #include <retro_inline.h>
37 
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41 
42 /* WATCHOUT: for c++ you may have to #define __STDC_LIMIT_MACROS 1 real early
43  * before #including this file, otherwise SIZE_MAX might not be defined
44  */
45 
46 #include <limits.h> /* for SIZE_MAX */
47 #if HAVE_STDINT_H
48 #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
49 #endif
50 #include <stdlib.h> /* for size_t, malloc(), etc */
51 #include "../share/compat.h"
52 
53 #ifndef SIZE_MAX
54 # ifndef SIZE_T_MAX
55 # ifdef _MSC_VER
56 # ifdef _WIN64
57 # define SIZE_T_MAX FLAC__U64L(0xffffffffffffffff)
58 # else
59 # define SIZE_T_MAX 0xffffffff
60 # endif
61 # else
62 # error
63 # endif
64 # endif
65 # define SIZE_MAX SIZE_T_MAX
66 #endif
67 
68 /* avoid malloc()ing 0 bytes, see:
69  * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
70 */
71 static INLINE void *safe_malloc_(size_t size)
72 {
73  /* malloc(0) is undefined; FLAC src convention is to always allocate */
74  if(!size)
75  size++;
76  return malloc(size);
77 }
78 
79 static INLINE void *safe_calloc_(size_t nmemb, size_t size)
80 {
81  if(!nmemb || !size)
82  return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
83  return calloc(nmemb, size);
84 }
85 
86 /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
87 
88 static INLINE void *safe_malloc_add_2op_(size_t size1, size_t size2)
89 {
90  size2 += size1;
91  if(size2 < size1)
92  return 0;
93  return safe_malloc_(size2);
94 }
95 
96 static INLINE void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
97 {
98  size2 += size1;
99  if(size2 < size1)
100  return 0;
101  size3 += size2;
102  if(size3 < size2)
103  return 0;
104  return safe_malloc_(size3);
105 }
106 
107 static INLINE void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
108 {
109  size2 += size1;
110  if(size2 < size1)
111  return 0;
112  size3 += size2;
113  if(size3 < size2)
114  return 0;
115  size4 += size3;
116  if(size4 < size3)
117  return 0;
118  return safe_malloc_(size4);
119 }
120 
121 void *safe_malloc_mul_2op_(size_t size1, size_t size2) ;
122 
123 static INLINE void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
124 {
125  if(!size1 || !size2 || !size3)
126  return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
127  if(size1 > SIZE_MAX / size2)
128  return 0;
129  size1 *= size2;
130  if(size1 > SIZE_MAX / size3)
131  return 0;
132  return malloc(size1*size3);
133 }
134 
135 /* size1*size2 + size3 */
136 static INLINE void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
137 {
138  if(!size1 || !size2)
139  return safe_malloc_(size3);
140  if(size1 > SIZE_MAX / size2)
141  return 0;
142  return safe_malloc_add_2op_(size1*size2, size3);
143 }
144 
145 /* size1 * (size2 + size3) */
146 static INLINE void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
147 {
148  if(!size1 || (!size2 && !size3))
149  return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
150  size2 += size3;
151  if(size2 < size3)
152  return 0;
153  if(size1 > SIZE_MAX / size2)
154  return 0;
155  return malloc(size1*size2);
156 }
157 
158 static INLINE void *safe_realloc_(void *ptr, size_t size)
159 {
160  void *oldptr = ptr;
161  void *newptr = realloc(ptr, size);
162  if(size > 0 && newptr == 0)
163  free(oldptr);
164  return newptr;
165 }
166 static INLINE void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
167 {
168  size2 += size1;
169  if(size2 < size1) {
170  free(ptr);
171  return 0;
172  }
173  return realloc(ptr, size2);
174 }
175 
176 static INLINE void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
177 {
178  size2 += size1;
179  if(size2 < size1)
180  return 0;
181  size3 += size2;
182  if(size3 < size2)
183  return 0;
184  return realloc(ptr, size3);
185 }
186 
187 static INLINE void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
188 {
189  size2 += size1;
190  if(size2 < size1)
191  return 0;
192  size3 += size2;
193  if(size3 < size2)
194  return 0;
195  size4 += size3;
196  if(size4 < size3)
197  return 0;
198  return realloc(ptr, size4);
199 }
200 
201 static INLINE void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
202 {
203  if(!size1 || !size2)
204  return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
205  if(size1 > SIZE_MAX / size2)
206  return 0;
207  return safe_realloc_(ptr, size1*size2);
208 }
209 
210 /* size1 * (size2 + size3) */
211 static INLINE void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
212 {
213  if(!size1 || (!size2 && !size3))
214  return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
215  size2 += size3;
216  if(size2 < size3)
217  return 0;
218  return safe_realloc_mul_2op_(ptr, size1, size2);
219 }
220 
221 #endif
static INLINE void * safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
Definition: alloc.h:187
static INLINE void * safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
Definition: alloc.h:201
const GLvoid * ptr
Definition: nx_glsym.h:242
#define INLINE
Definition: retro_inline.h:35
static INLINE void * safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
Definition: alloc.h:107
Configuration options (set of defines)
void * malloc(YYSIZE_T)
static INLINE void * safe_malloc_(size_t size)
Definition: alloc.h:71
GLsizeiptr size
Definition: glext.h:6559
static INLINE void * safe_calloc_(size_t nmemb, size_t size)
Definition: alloc.h:79
static INLINE void * safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
Definition: alloc.h:176
static INLINE void * safe_realloc_(void *ptr, size_t size)
Definition: alloc.h:158
static INLINE void * safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
Definition: alloc.h:146
static INLINE void * safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
Definition: alloc.h:136
static INLINE void * safe_malloc_add_2op_(size_t size1, size_t size2)
Definition: alloc.h:88
#define SIZE_MAX
Definition: alloc.h:65
static INLINE void * safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
Definition: alloc.h:166
void free(void *)
static INLINE void * safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
Definition: alloc.h:96
static INLINE void * safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
Definition: alloc.h:211
void * safe_malloc_mul_2op_(size_t size1, size_t size2)
static INLINE void * safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
Definition: alloc.h:123