LMMS
Loading...
Searching...
No Matches
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-2014 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#ifdef HAVE_CONFIG_H
37# include <config.h>
38#endif
39
40/* WATCHOUT: for c++ you may have to #define __STDC_LIMIT_MACROS 1 real early
41 * before #including this file, otherwise SIZE_MAX might not be defined
42 */
43
44// JUCE: removed as JUCE already includes standard headers and including
45// these in FlacNamespace will cause problems
46
47//#include <limits.h> /* for SIZE_MAX */
48//#if HAVE_STDINT_H
49//#include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
50//#endif
51//#include <stdlib.h> /* for size_t, malloc(), etc */
52#include "compat.h"
53
54#ifndef SIZE_MAX
55# ifndef SIZE_T_MAX
56# ifdef _MSC_VER
57# ifdef _WIN64
58# define SIZE_T_MAX 0xffffffffffffffffui64
59# else
60# define SIZE_T_MAX 0xffffffff
61# endif
62# else
63# error
64# endif
65# endif
66# define SIZE_MAX SIZE_T_MAX
67#endif
68
69/* avoid malloc()ing 0 bytes, see:
70 * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
71*/
72static inline void *safe_malloc_(size_t size)
73{
74 /* malloc(0) is undefined; FLAC src convention is to always allocate */
75 if(!size)
76 size++;
77 return malloc(size);
78}
79
80static inline void *safe_calloc_(size_t nmemb, size_t size)
81{
82 if(!nmemb || !size)
83 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
84 return calloc(nmemb, size);
85}
86
87/*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
88
89static inline void *safe_malloc_add_2op_(size_t size1, size_t size2)
90{
91 size2 += size1;
92 if(size2 < size1)
93 return 0;
94 return safe_malloc_(size2);
95}
96
97static inline void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
98{
99 size2 += size1;
100 if(size2 < size1)
101 return 0;
102 size3 += size2;
103 if(size3 < size2)
104 return 0;
105 return safe_malloc_(size3);
106}
107
108static inline void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
109{
110 size2 += size1;
111 if(size2 < size1)
112 return 0;
113 size3 += size2;
114 if(size3 < size2)
115 return 0;
116 size4 += size3;
117 if(size4 < size3)
118 return 0;
119 return safe_malloc_(size4);
120}
121
122void *safe_malloc_mul_2op_(size_t size1, size_t size2) ;
123
124static inline void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
125{
126 if(!size1 || !size2 || !size3)
127 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
128 if(size1 > SIZE_MAX / size2)
129 return 0;
130 size1 *= size2;
131 if(size1 > SIZE_MAX / size3)
132 return 0;
133 return malloc(size1*size3);
134}
135
136/* size1*size2 + size3 */
137static inline void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
138{
139 if(!size1 || !size2)
140 return safe_malloc_(size3);
141 if(size1 > SIZE_MAX / size2)
142 return 0;
143 return safe_malloc_add_2op_(size1*size2, size3);
144}
145
146/* size1 * (size2 + size3) */
147static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
148{
149 if(!size1 || (!size2 && !size3))
150 return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
151 size2 += size3;
152 if(size2 < size3)
153 return 0;
154 if(size1 > SIZE_MAX / size2)
155 return 0;
156 return malloc(size1*size2);
157}
158
159static inline void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
160{
161 size2 += size1;
162 if(size2 < size1)
163 return 0;
164 return realloc(ptr, size2);
165}
166
167static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
168{
169 size2 += size1;
170 if(size2 < size1)
171 return 0;
172 size3 += size2;
173 if(size3 < size2)
174 return 0;
175 return realloc(ptr, size3);
176}
177
178static inline void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
179{
180 size2 += size1;
181 if(size2 < size1)
182 return 0;
183 size3 += size2;
184 if(size3 < size2)
185 return 0;
186 size4 += size3;
187 if(size4 < size3)
188 return 0;
189 return realloc(ptr, size4);
190}
191
192static inline void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
193{
194 if(!size1 || !size2)
195 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
196 if(size1 > SIZE_MAX / size2)
197 return 0;
198 return realloc(ptr, size1*size2);
199}
200
201/* size1 * (size2 + size3) */
202static inline void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
203{
204 if(!size1 || (!size2 && !size3))
205 return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
206 size2 += size3;
207 if(size2 < size3)
208 return 0;
209 return safe_realloc_mul_2op_(ptr, size1, size2);
210}
211
212#endif
static void * safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
Definition alloc.h:97
static void * safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
Definition alloc.h:178
static void * safe_malloc_add_2op_(size_t size1, size_t size2)
Definition alloc.h:89
static void * safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
Definition alloc.h:124
#define SIZE_MAX
Definition alloc.h:66
static void * safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
Definition alloc.h:137
static void * safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
Definition alloc.h:202
static void * safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
Definition alloc.h:192
static void * safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
Definition alloc.h:147
static void * safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
Definition alloc.h:108
void * safe_malloc_mul_2op_(size_t size1, size_t size2)
static void * safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
Definition alloc.h:159
static void * safe_malloc_(size_t size)
Definition alloc.h:72
static void * safe_calloc_(size_t nmemb, size_t size)
Definition alloc.h:80
static void * safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
Definition alloc.h:167
ulg size
Definition extract.c:2350
char * malloc()