Audio plugin host https://kx.studio/carla
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.

213 lines
5.8KB

  1. /* alloc - Convenience routines for safely allocating memory
  2. * Copyright (C) 2007 Josh Coalson
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef FLAC__SHARE__ALLOC_H
  19. #define FLAC__SHARE__ALLOC_H
  20. #if HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. /* WATCHOUT: for c++ you may have to #define __STDC_LIMIT_MACROS 1 real early
  24. * before #including this file, otherwise SIZE_MAX might not be defined
  25. */
  26. #include <limits.h> /* for SIZE_MAX */
  27. #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
  28. #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
  29. #endif
  30. #include <stdlib.h> /* for size_t, malloc(), etc */
  31. #ifndef SIZE_MAX
  32. # ifndef SIZE_T_MAX
  33. # ifdef _MSC_VER
  34. # define SIZE_T_MAX UINT_MAX
  35. # else
  36. # error
  37. # endif
  38. # endif
  39. # define SIZE_MAX SIZE_T_MAX
  40. #endif
  41. #ifndef FLaC__INLINE
  42. #define FLaC__INLINE
  43. #endif
  44. /* avoid malloc()ing 0 bytes, see:
  45. * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
  46. */
  47. static FLaC__INLINE void *safe_malloc_(size_t size)
  48. {
  49. /* malloc(0) is undefined; FLAC src convention is to always allocate */
  50. if(!size)
  51. size++;
  52. return malloc(size);
  53. }
  54. static FLaC__INLINE void *safe_calloc_(size_t nmemb, size_t size)
  55. {
  56. if(!nmemb || !size)
  57. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  58. return calloc(nmemb, size);
  59. }
  60. /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
  61. static FLaC__INLINE void *safe_malloc_add_2op_(size_t size1, size_t size2)
  62. {
  63. size2 += size1;
  64. if(size2 < size1)
  65. return 0;
  66. return safe_malloc_(size2);
  67. }
  68. static FLaC__INLINE void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
  69. {
  70. size2 += size1;
  71. if(size2 < size1)
  72. return 0;
  73. size3 += size2;
  74. if(size3 < size2)
  75. return 0;
  76. return safe_malloc_(size3);
  77. }
  78. static FLaC__INLINE void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
  79. {
  80. size2 += size1;
  81. if(size2 < size1)
  82. return 0;
  83. size3 += size2;
  84. if(size3 < size2)
  85. return 0;
  86. size4 += size3;
  87. if(size4 < size3)
  88. return 0;
  89. return safe_malloc_(size4);
  90. }
  91. static FLaC__INLINE void *safe_malloc_mul_2op_(size_t size1, size_t size2)
  92. #if 0
  93. needs support for cases where sizeof(size_t) != 4
  94. {
  95. /* could be faster #ifdef'ing off SIZEOF_SIZE_T */
  96. if(sizeof(size_t) == 4) {
  97. if ((double)size1 * (double)size2 < 4294967296.0)
  98. return malloc(size1*size2);
  99. }
  100. return 0;
  101. }
  102. #else
  103. /* better? */
  104. {
  105. if(!size1 || !size2)
  106. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  107. if(size1 > SIZE_MAX / size2)
  108. return 0;
  109. return malloc(size1*size2);
  110. }
  111. #endif
  112. static FLaC__INLINE void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
  113. {
  114. if(!size1 || !size2 || !size3)
  115. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  116. if(size1 > SIZE_MAX / size2)
  117. return 0;
  118. size1 *= size2;
  119. if(size1 > SIZE_MAX / size3)
  120. return 0;
  121. return malloc(size1*size3);
  122. }
  123. /* size1*size2 + size3 */
  124. static FLaC__INLINE void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
  125. {
  126. if(!size1 || !size2)
  127. return safe_malloc_(size3);
  128. if(size1 > SIZE_MAX / size2)
  129. return 0;
  130. return safe_malloc_add_2op_(size1*size2, size3);
  131. }
  132. /* size1 * (size2 + size3) */
  133. static FLaC__INLINE void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
  134. {
  135. if(!size1 || (!size2 && !size3))
  136. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  137. size2 += size3;
  138. if(size2 < size3)
  139. return 0;
  140. return safe_malloc_mul_2op_(size1, size2);
  141. }
  142. static FLaC__INLINE void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
  143. {
  144. size2 += size1;
  145. if(size2 < size1)
  146. return 0;
  147. return realloc(ptr, size2);
  148. }
  149. static FLaC__INLINE void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
  150. {
  151. size2 += size1;
  152. if(size2 < size1)
  153. return 0;
  154. size3 += size2;
  155. if(size3 < size2)
  156. return 0;
  157. return realloc(ptr, size3);
  158. }
  159. static FLaC__INLINE void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
  160. {
  161. size2 += size1;
  162. if(size2 < size1)
  163. return 0;
  164. size3 += size2;
  165. if(size3 < size2)
  166. return 0;
  167. size4 += size3;
  168. if(size4 < size3)
  169. return 0;
  170. return realloc(ptr, size4);
  171. }
  172. static FLaC__INLINE void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
  173. {
  174. if(!size1 || !size2)
  175. return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
  176. if(size1 > SIZE_MAX / size2)
  177. return 0;
  178. return realloc(ptr, size1*size2);
  179. }
  180. /* size1 * (size2 + size3) */
  181. static FLaC__INLINE void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
  182. {
  183. if(!size1 || (!size2 && !size3))
  184. return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
  185. size2 += size3;
  186. if(size2 < size3)
  187. return 0;
  188. return safe_realloc_mul_2op_(ptr, size1, size2);
  189. }
  190. #endif