flac_share_alloc.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 __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. /* ppgb 20141231
  32. #ifndef SIZE_MAX
  33. # ifndef SIZE_T_MAX
  34. # ifdef _MSC_VER
  35. # define SIZE_T_MAX UINT_MAX
  36. # else
  37. # error
  38. # endif
  39. # endif
  40. # define SIZE_MAX SIZE_T_MAX
  41. #endif
  42. */
  43. #ifndef FLaC__INLINE
  44. #define FLaC__INLINE inline // ppgb 20071120
  45. #endif
  46. /* avoid malloc()ing 0 bytes, see:
  47. * https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
  48. */
  49. static FLaC__INLINE void *safe_malloc_(size_t size)
  50. {
  51. /* malloc(0) is undefined; FLAC src convention is to always allocate */
  52. if(!size)
  53. size++;
  54. return malloc(size);
  55. }
  56. static FLaC__INLINE void *safe_calloc_(size_t nmemb, size_t size)
  57. {
  58. if(!nmemb || !size)
  59. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  60. return calloc(nmemb, size);
  61. }
  62. /*@@@@ there's probably a better way to prevent overflows when allocating untrusted sums but this works for now */
  63. static FLaC__INLINE void *safe_malloc_add_2op_(size_t size1, size_t size2)
  64. {
  65. size2 += size1;
  66. if(size2 < size1)
  67. return 0;
  68. return safe_malloc_(size2);
  69. }
  70. static FLaC__INLINE void *safe_malloc_add_3op_(size_t size1, size_t size2, size_t size3)
  71. {
  72. size2 += size1;
  73. if(size2 < size1)
  74. return 0;
  75. size3 += size2;
  76. if(size3 < size2)
  77. return 0;
  78. return safe_malloc_(size3);
  79. }
  80. static FLaC__INLINE void *safe_malloc_add_4op_(size_t size1, size_t size2, size_t size3, size_t size4)
  81. {
  82. size2 += size1;
  83. if(size2 < size1)
  84. return 0;
  85. size3 += size2;
  86. if(size3 < size2)
  87. return 0;
  88. size4 += size3;
  89. if(size4 < size3)
  90. return 0;
  91. return safe_malloc_(size4);
  92. }
  93. static FLaC__INLINE void *safe_malloc_mul_2op_(size_t size1, size_t size2)
  94. #if 0
  95. needs support for cases where sizeof(size_t) != 4
  96. {
  97. /* could be faster #ifdef'ing off SIZEOF_SIZE_T */
  98. if(sizeof(size_t) == 4) {
  99. if ((double)size1 * (double)size2 < 4294967296.0)
  100. return malloc(size1*size2);
  101. }
  102. return 0;
  103. }
  104. #else
  105. /* better? */
  106. {
  107. if(!size1 || !size2)
  108. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  109. if(size1 > SIZE_MAX / size2)
  110. return 0;
  111. return malloc(size1*size2);
  112. }
  113. #endif
  114. static FLaC__INLINE void *safe_malloc_mul_3op_(size_t size1, size_t size2, size_t size3)
  115. {
  116. if(!size1 || !size2 || !size3)
  117. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  118. if(size1 > SIZE_MAX / size2)
  119. return 0;
  120. size1 *= size2;
  121. if(size1 > SIZE_MAX / size3)
  122. return 0;
  123. return malloc(size1*size3);
  124. }
  125. /* size1*size2 + size3 */
  126. static FLaC__INLINE void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
  127. {
  128. if(!size1 || !size2)
  129. return safe_malloc_(size3);
  130. if(size1 > SIZE_MAX / size2)
  131. return 0;
  132. return safe_malloc_add_2op_(size1*size2, size3);
  133. }
  134. /* size1 * (size2 + size3) */
  135. static FLaC__INLINE void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
  136. {
  137. if(!size1 || (!size2 && !size3))
  138. return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
  139. size2 += size3;
  140. if(size2 < size3)
  141. return 0;
  142. return safe_malloc_mul_2op_(size1, size2);
  143. }
  144. static FLaC__INLINE void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
  145. {
  146. size2 += size1;
  147. if(size2 < size1)
  148. return 0;
  149. return realloc(ptr, size2);
  150. }
  151. static FLaC__INLINE void *safe_realloc_add_3op_(void *ptr, size_t size1, size_t size2, size_t size3)
  152. {
  153. size2 += size1;
  154. if(size2 < size1)
  155. return 0;
  156. size3 += size2;
  157. if(size3 < size2)
  158. return 0;
  159. return realloc(ptr, size3);
  160. }
  161. static FLaC__INLINE void *safe_realloc_add_4op_(void *ptr, size_t size1, size_t size2, size_t size3, size_t size4)
  162. {
  163. size2 += size1;
  164. if(size2 < size1)
  165. return 0;
  166. size3 += size2;
  167. if(size3 < size2)
  168. return 0;
  169. size4 += size3;
  170. if(size4 < size3)
  171. return 0;
  172. return realloc(ptr, size4);
  173. }
  174. static FLaC__INLINE void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
  175. {
  176. if(!size1 || !size2)
  177. return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
  178. if(size1 > SIZE_MAX / size2)
  179. return 0;
  180. return realloc(ptr, size1*size2);
  181. }
  182. /* size1 * (size2 + size3) */
  183. static FLaC__INLINE void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
  184. {
  185. if(!size1 || (!size2 && !size3))
  186. return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
  187. size2 += size3;
  188. if(size2 < size3)
  189. return 0;
  190. return safe_realloc_mul_2op_(ptr, size1, size2);
  191. }
  192. #endif