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
6.2KB

  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. #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 0xffffffffffffffffui64
  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. /* avoid malloc()ing 0 bytes, see:
  63. * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
  64. */
  65. static inline void *safe_malloc_(size_t size)
  66. {
  67. /* malloc(0) is undefined; FLAC src convention is to always allocate */
  68. if(!size)
  69. size++;
  70. return malloc(size);
  71. }
  72. static inline void *safe_calloc_(size_t nmemb, size_t size)
  73. {
  74. if(!nmemb || !size)
  75. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  76. return calloc(nmemb, size);
  77. }
  78. /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
  79. static inline void *safe_malloc_add_2op_(size_t size1, size_t size2)
  80. {
  81. size2 += size1;
  82. if(size2 < size1)
  83. return 0;
  84. return safe_malloc_(size2);
  85. }
  86. static inline void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
  87. {
  88. size2 += size1;
  89. if(size2 < size1)
  90. return 0;
  91. size3 += size2;
  92. if(size3 < size2)
  93. return 0;
  94. return safe_malloc_(size3);
  95. }
  96. static inline void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
  97. {
  98. size2 += size1;
  99. if(size2 < size1)
  100. return 0;
  101. size3 += size2;
  102. if(size3 < size2)
  103. return 0;
  104. size4 += size3;
  105. if(size4 < size3)
  106. return 0;
  107. return safe_malloc_(size4);
  108. }
  109. void *safe_malloc_mul_2op_(size_t size1, size_t size2) ;
  110. static inline void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
  111. {
  112. if(!size1 || !size2 || !size3)
  113. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  114. if(size1 > SIZE_MAX / size2)
  115. return 0;
  116. size1 *= size2;
  117. if(size1 > SIZE_MAX / size3)
  118. return 0;
  119. return malloc(size1*size3);
  120. }
  121. /* size1*size2 + size3 */
  122. static inline void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
  123. {
  124. if(!size1 || !size2)
  125. return safe_malloc_(size3);
  126. if(size1 > SIZE_MAX / size2)
  127. return 0;
  128. return safe_malloc_add_2op_(size1*size2, size3);
  129. }
  130. /* size1 * (size2 + size3) */
  131. static inline void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
  132. {
  133. if(!size1 || (!size2 && !size3))
  134. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  135. size2 += size3;
  136. if(size2 < size3)
  137. return 0;
  138. if(size1 > SIZE_MAX / size2)
  139. return 0;
  140. return malloc(size1*size2);
  141. }
  142. static 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 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 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 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 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