| 1 | /* alloc - Convenience routines for safely allocating memory |
| 2 | * Copyright (C) 2007-2009 Josh Coalson |
| 3 | * Copyright (C) 2011-2025 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 | //ppgb #ifdef HAVE_CONFIG_H |
| 37 | #include "flac_config.h" |
| 38 | //ppgb #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 | #include <limits.h> /* for SIZE_MAX */ |
| 45 | #ifdef HAVE_STDINT_H |
| 46 | #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */ |
| 47 | #endif |
| 48 | #include <stdlib.h> /* for size_t, malloc(), etc */ |
| 49 | #include "flac_share_compat.h" |
| 50 | |
| 51 | #ifndef SIZE_MAX |
| 52 | # ifndef SIZE_T_MAX |
| 53 | # ifdef _MSC_VER |
| 54 | # ifdef _WIN64 |
| 55 | # define SIZE_T_MAX FLAC__U64L(0xffffffffffffffff) |
| 56 | # else |
| 57 | # define SIZE_T_MAX 0xffffffff |
| 58 | # endif |
| 59 | # else |
| 60 | # error |
| 61 | # endif |
| 62 | # endif |
| 63 | # define SIZE_MAX SIZE_T_MAX |
| 64 | #endif |
| 65 | |
| 66 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 67 | |
| 68 | extern int alloc_check_threshold, alloc_check_counter, alloc_check_keep_failing; |
| 69 | |
| 70 | static inline int alloc_check(void) { |
| 71 | if(alloc_check_threshold == INT32_MAX) |
| 72 | return 0; |
| 73 | else if(alloc_check_counter++ == alloc_check_threshold) |
| 74 | return 1; |
| 75 | else if(alloc_check_keep_failing && (alloc_check_counter > alloc_check_threshold)) |
| 76 | return 1; |
| 77 | else { |
| 78 | return 0; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #endif |
| 83 | |
| 84 | /* avoid malloc()ing 0 bytes, see: |
| 85 | * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003 |
| 86 | */ |
| 87 | |
| 88 | static inline void *safe_malloc_(size_t size) |
| 89 | { |
| 90 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 91 | /* Fail if requested */ |
| 92 | if(alloc_check()) |
| 93 | return NULL; |
| 94 | #endif |
| 95 | /* malloc(0) is undefined; FLAC src convention is to always allocate */ |
| 96 | if(!size) |
| 97 | size++; |
| 98 | return malloc(size); |
| 99 | } |
| 100 | |
| 101 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 102 | static inline void *malloc_(size_t size) |
| 103 | { |
| 104 | /* Fail if requested */ |
| 105 | if(alloc_check()) |
| 106 | return NULL; |
| 107 | return malloc(size); |
| 108 | } |
| 109 | #else |
| 110 | #define malloc_ malloc |
| 111 | #endif |
| 112 | |
| 113 | |
| 114 | |
| 115 | static inline void *safe_calloc_(size_t nmemb, size_t size) |
| 116 | { |
| 117 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 118 | /* Fail if requested */ |
| 119 | if(alloc_check()) |
| 120 | return NULL; |
| 121 | #endif |
| 122 | if(!nmemb || !size) |
| 123 | return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ |
| 124 | return calloc(nmemb, size); |
| 125 | } |
| 126 | |
| 127 | /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */ |
| 128 | |
| 129 | static inline void *safe_malloc_add_2op_(size_t size1, size_t size2) |
| 130 | { |
| 131 | size2 += size1; |
| 132 | if(size2 < size1) |
| 133 | return 0; |
| 134 | return safe_malloc_(size2); |
| 135 | } |
| 136 | |
| 137 | static inline void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3) |
| 138 | { |
| 139 | size2 += size1; |
| 140 | if(size2 < size1) |
| 141 | return 0; |
| 142 | size3 += size2; |
| 143 | if(size3 < size2) |
| 144 | return 0; |
| 145 | return safe_malloc_(size3); |
| 146 | } |
| 147 | |
| 148 | static inline void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4) |
| 149 | { |
| 150 | size2 += size1; |
| 151 | if(size2 < size1) |
| 152 | return 0; |
| 153 | size3 += size2; |
| 154 | if(size3 < size2) |
| 155 | return 0; |
| 156 | size4 += size3; |
| 157 | if(size4 < size3) |
| 158 | return 0; |
| 159 | return safe_malloc_(size4); |
| 160 | } |
| 161 | |
| 162 | void *safe_malloc_mul_2op_(size_t size1, size_t size2) ; |
| 163 | |
| 164 | static inline void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3) |
| 165 | { |
| 166 | if(!size1 || !size2 || !size3) |
| 167 | return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ |
| 168 | if(size1 > SIZE_MAX / size2) |
| 169 | return 0; |
| 170 | size1 *= size2; |
| 171 | if(size1 > SIZE_MAX / size3) |
| 172 | return 0; |
| 173 | return malloc_(size1*size3); |
| 174 | } |
| 175 | |
| 176 | /* size1*size2 + size3 */ |
| 177 | static inline void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3) |
| 178 | { |
| 179 | if(!size1 || !size2) |
| 180 | return safe_malloc_(size3); |
| 181 | if(size1 > SIZE_MAX / size2) |
| 182 | return 0; |
| 183 | return safe_malloc_add_2op_(size1*size2, size3); |
| 184 | } |
| 185 | |
| 186 | /* size1 * (size2 + size3) */ |
| 187 | static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3) |
| 188 | { |
| 189 | if(!size1 || (!size2 && !size3)) |
| 190 | return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */ |
| 191 | size2 += size3; |
| 192 | if(size2 < size3) |
| 193 | return 0; |
| 194 | if(size1 > SIZE_MAX / size2) |
| 195 | return 0; |
| 196 | return malloc_(size1*size2); |
| 197 | } |
| 198 | |
| 199 | static inline void *safe_realloc_(void *ptr, size_t size) |
| 200 | { |
| 201 | void *oldptr; |
| 202 | void *newptr; |
| 203 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 204 | /* Fail if requested */ |
| 205 | if(alloc_check() && size > 0) { |
| 206 | free(ptr); |
| 207 | return NULL; |
| 208 | } |
| 209 | #endif |
| 210 | oldptr = ptr; |
| 211 | newptr = realloc(ptr, size); |
| 212 | if(size > 0 && newptr == 0) |
| 213 | free(oldptr); |
| 214 | return newptr; |
| 215 | } |
| 216 | |
| 217 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 218 | static inline void *realloc_(void *ptr, size_t size) |
| 219 | { |
| 220 | /* Fail if requested */ |
| 221 | if(alloc_check()) |
| 222 | return NULL; |
| 223 | return realloc(ptr, size); |
| 224 | } |
| 225 | #else |
| 226 | #define realloc_ realloc |
| 227 | #endif |
| 228 | |
| 229 | |
| 230 | static inline void *safe_realloc_nofree_add_2op_(void *ptr, size_t size1, size_t size2) |
| 231 | { |
| 232 | size2 += size1; |
| 233 | if(size2 < size1) |
| 234 | return 0; |
| 235 | return realloc_(ptr, size2); |
| 236 | } |
| 237 | |
| 238 | static inline void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3) |
| 239 | { |
| 240 | size2 += size1; |
| 241 | if(size2 < size1) { |
| 242 | free(ptr); |
| 243 | return 0; |
| 244 | } |
| 245 | size3 += size2; |
| 246 | if(size3 < size2) { |
| 247 | free(ptr); |
| 248 | return 0; |
| 249 | } |
| 250 | return safe_realloc_(ptr, size3); |
| 251 | } |
| 252 | |
| 253 | static inline void *safe_realloc_nofree_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3) |
| 254 | { |
| 255 | size2 += size1; |
| 256 | if(size2 < size1) |
| 257 | return 0; |
| 258 | size3 += size2; |
| 259 | if(size3 < size2) |
| 260 | return 0; |
| 261 | return realloc_(ptr, size3); |
| 262 | } |
| 263 | |
| 264 | static inline void *safe_realloc_nofree_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4) |
| 265 | { |
| 266 | size2 += size1; |
| 267 | if(size2 < size1) |
| 268 | return 0; |
| 269 | size3 += size2; |
| 270 | if(size3 < size2) |
| 271 | return 0; |
| 272 | size4 += size3; |
| 273 | if(size4 < size3) |
| 274 | return 0; |
| 275 | return realloc_(ptr, size4); |
| 276 | } |
| 277 | |
| 278 | static inline void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2) |
| 279 | { |
| 280 | if(!size1 || !size2) |
| 281 | return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */ |
| 282 | if(size1 > SIZE_MAX / size2) { |
| 283 | free(ptr); |
| 284 | return 0; |
| 285 | } |
| 286 | return safe_realloc_(ptr, size1*size2); |
| 287 | } |
| 288 | |
| 289 | static inline void *safe_realloc_nofree_mul_2op_(void *ptr, size_t size1, size_t size2) |
| 290 | { |
| 291 | if(!size1 || !size2) |
| 292 | return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */ |
| 293 | if(size1 > SIZE_MAX / size2) |
| 294 | return 0; |
| 295 | return realloc_(ptr, size1*size2); |
| 296 | } |
| 297 | |
| 298 | /* size1 * (size2 + size3) */ |
| 299 | static inline void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3) |
| 300 | { |
| 301 | if(!size1 || (!size2 && !size3)) |
| 302 | return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */ |
| 303 | size2 += size3; |
| 304 | if(size2 < size3) { |
| 305 | free(ptr); |
| 306 | return 0; |
| 307 | } |
| 308 | return safe_realloc_mul_2op_(ptr, size1, size2); |
| 309 | } |
| 310 | |
| 311 | /* size1 * (size2 + size3) */ |
| 312 | static inline void *safe_realloc_nofree_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3) |
| 313 | { |
| 314 | if(!size1 || (!size2 && !size3)) |
| 315 | return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */ |
| 316 | size2 += size3; |
| 317 | if(size2 < size3) |
| 318 | return 0; |
| 319 | return safe_realloc_nofree_mul_2op_(ptr, size1, size2); |
| 320 | } |
| 321 | |
| 322 | #endif |