1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 #ifndef __LINUX_OVERFLOW_H
3 #define __LINUX_OVERFLOW_H
5 #include <linux/compiler.h>
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:
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)
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...).
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
28 * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
29 * credit to Christian Biere.
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))
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
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); \
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); \
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); \
72 * array_size() - Calculate size of 2-dimensional array.
77 * Calculates size of 2-dimensional array: @a * @b.
79 * Returns: number of bytes needed to represent the array or SIZE_MAX on
82 static inline __must_check
size_t array_size(size_t a
, size_t b
)
86 if (check_mul_overflow(a
, b
, &bytes
))
93 * array3_size() - Calculate size of 3-dimensional array.
99 * Calculates size of 3-dimensional array: @a * @b * @c.
101 * Returns: number of bytes needed to represent the array or SIZE_MAX on
104 static inline __must_check
size_t array3_size(size_t a
, size_t b
, size_t c
)
108 if (check_mul_overflow(a
, b
, &bytes
))
110 if (check_mul_overflow(bytes
, c
, &bytes
))
116 static inline __must_check
size_t __ab_c_size(size_t n
, size_t size
, size_t c
)
120 if (check_mul_overflow(n
, size
, &bytes
))
122 if (check_add_overflow(bytes
, c
, &bytes
))
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.
134 * Calculates size of memory needed for structure @p followed by an
135 * array of @n @member elements.
137 * Return: number of bytes needed or SIZE_MAX on overflow.
139 #define struct_size(p, member, n) \
141 sizeof(*(p)->member) + __must_be_array((p)->member),\
144 #endif /* __LINUX_OVERFLOW_H */