overflow.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. #ifndef __LINUX_OVERFLOW_H
  3. #define __LINUX_OVERFLOW_H
  4. #include <linux/compiler.h>
  5. /*
  6. * We need to compute the minimum and maximum values representable in a given
  7. * type. These macros may also be useful elsewhere. It would seem more obvious
  8. * to do something like:
  9. *
  10. * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
  11. * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
  12. *
  13. * Unfortunately, the middle expressions, strictly speaking, have
  14. * undefined behaviour, and at least some versions of gcc warn about
  15. * the type_max expression (but not if -fsanitize=undefined is in
  16. * effect; in that case, the warning is deferred to runtime...).
  17. *
  18. * The slightly excessive casting in type_min is to make sure the
  19. * macros also produce sensible values for the exotic type _Bool. [The
  20. * overflow checkers only almost work for _Bool, but that's
  21. * a-feature-not-a-bug, since people shouldn't be doing arithmetic on
  22. * _Bools. Besides, the gcc builtins don't allow _Bool* as third
  23. * argument.]
  24. *
  25. * Idea stolen from
  26. * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
  27. * credit to Christian Biere.
  28. */
  29. #define is_signed_type(type) (((type)(-1)) < (type)1)
  30. #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
  31. #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
  32. #define type_min(T) ((T)((T)-type_max(T)-(T)1))
  33. /*
  34. * For simplicity and code hygiene, the fallback code below insists on
  35. * a, b and *d having the same type (similar to the min() and max()
  36. * macros), whereas gcc's type-generic overflow checkers accept
  37. * different types. Hence we don't just make check_add_overflow an
  38. * alias for __builtin_add_overflow, but add type checks similar to
  39. * below.
  40. */
  41. #define check_add_overflow(a, b, d) ({ \
  42. typeof(a) __a = (a); \
  43. typeof(b) __b = (b); \
  44. typeof(d) __d = (d); \
  45. (void) (&__a == &__b); \
  46. (void) (&__a == __d); \
  47. __builtin_add_overflow(__a, __b, __d); \
  48. })
  49. #define check_sub_overflow(a, b, d) ({ \
  50. typeof(a) __a = (a); \
  51. typeof(b) __b = (b); \
  52. typeof(d) __d = (d); \
  53. (void) (&__a == &__b); \
  54. (void) (&__a == __d); \
  55. __builtin_sub_overflow(__a, __b, __d); \
  56. })
  57. #define check_mul_overflow(a, b, d) ({ \
  58. typeof(a) __a = (a); \
  59. typeof(b) __b = (b); \
  60. typeof(d) __d = (d); \
  61. (void) (&__a == &__b); \
  62. (void) (&__a == __d); \
  63. __builtin_mul_overflow(__a, __b, __d); \
  64. })
  65. /**
  66. * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
  67. * @factor1: first factor
  68. * @factor2: second factor
  69. *
  70. * Returns: calculate @factor1 * @factor2, both promoted to size_t,
  71. * with any overflow causing the return value to be SIZE_MAX. The
  72. * lvalue must be size_t to avoid implicit type conversion.
  73. */
  74. static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
  75. {
  76. size_t bytes;
  77. if (check_mul_overflow(factor1, factor2, &bytes))
  78. return SIZE_MAX;
  79. return bytes;
  80. }
  81. /**
  82. * array_size() - Calculate size of 2-dimensional array.
  83. *
  84. * @a: dimension one
  85. * @b: dimension two
  86. *
  87. * Calculates size of 2-dimensional array: @a * @b.
  88. *
  89. * Returns: number of bytes needed to represent the array or SIZE_MAX on
  90. * overflow.
  91. */
  92. static inline __must_check size_t array_size(size_t a, size_t b)
  93. {
  94. size_t bytes;
  95. if (check_mul_overflow(a, b, &bytes))
  96. return SIZE_MAX;
  97. return bytes;
  98. }
  99. /**
  100. * array3_size() - Calculate size of 3-dimensional array.
  101. *
  102. * @a: dimension one
  103. * @b: dimension two
  104. * @c: dimension three
  105. *
  106. * Calculates size of 3-dimensional array: @a * @b * @c.
  107. *
  108. * Returns: number of bytes needed to represent the array or SIZE_MAX on
  109. * overflow.
  110. */
  111. static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
  112. {
  113. size_t bytes;
  114. if (check_mul_overflow(a, b, &bytes))
  115. return SIZE_MAX;
  116. if (check_mul_overflow(bytes, c, &bytes))
  117. return SIZE_MAX;
  118. return bytes;
  119. }
  120. static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
  121. {
  122. size_t bytes;
  123. if (check_mul_overflow(n, size, &bytes))
  124. return SIZE_MAX;
  125. if (check_add_overflow(bytes, c, &bytes))
  126. return SIZE_MAX;
  127. return bytes;
  128. }
  129. /**
  130. * struct_size() - Calculate size of structure with trailing array.
  131. * @p: Pointer to the structure.
  132. * @member: Name of the array member.
  133. * @n: Number of elements in the array.
  134. *
  135. * Calculates size of memory needed for structure @p followed by an
  136. * array of @n @member elements.
  137. *
  138. * Return: number of bytes needed or SIZE_MAX on overflow.
  139. */
  140. #define struct_size(p, member, n) \
  141. __ab_c_size(n, \
  142. sizeof(*(p)->member) + __must_be_array((p)->member),\
  143. sizeof(*(p)))
  144. #endif /* __LINUX_OVERFLOW_H */