The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
8.5KB

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