overflow.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * array_size() - Calculate size of 2-dimensional array.
  67. *
  68. * @a: dimension one
  69. * @b: dimension two
  70. *
  71. * Calculates size of 2-dimensional array: @a * @b.
  72. *
  73. * Returns: number of bytes needed to represent the array or SIZE_MAX on
  74. * overflow.
  75. */
  76. static inline __must_check size_t array_size(size_t a, size_t b)
  77. {
  78. size_t bytes;
  79. if (check_mul_overflow(a, b, &bytes))
  80. return SIZE_MAX;
  81. return bytes;
  82. }
  83. /**
  84. * array3_size() - Calculate size of 3-dimensional array.
  85. *
  86. * @a: dimension one
  87. * @b: dimension two
  88. * @c: dimension three
  89. *
  90. * Calculates size of 3-dimensional array: @a * @b * @c.
  91. *
  92. * Returns: number of bytes needed to represent the array or SIZE_MAX on
  93. * overflow.
  94. */
  95. static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
  96. {
  97. size_t bytes;
  98. if (check_mul_overflow(a, b, &bytes))
  99. return SIZE_MAX;
  100. if (check_mul_overflow(bytes, c, &bytes))
  101. return SIZE_MAX;
  102. return bytes;
  103. }
  104. static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
  105. {
  106. size_t bytes;
  107. if (check_mul_overflow(n, size, &bytes))
  108. return SIZE_MAX;
  109. if (check_add_overflow(bytes, c, &bytes))
  110. return SIZE_MAX;
  111. return bytes;
  112. }
  113. /**
  114. * struct_size() - Calculate size of structure with trailing array.
  115. * @p: Pointer to the structure.
  116. * @member: Name of the array member.
  117. * @n: Number of elements in the array.
  118. *
  119. * Calculates size of memory needed for structure @p followed by an
  120. * array of @n @member elements.
  121. *
  122. * Return: number of bytes needed or SIZE_MAX on overflow.
  123. */
  124. #define struct_size(p, member, n) \
  125. __ab_c_size(n, \
  126. sizeof(*(p)->member) + __must_be_array((p)->member),\
  127. sizeof(*(p)))
  128. #endif /* __LINUX_OVERFLOW_H */