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