alloc.h 8.1 KB

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