1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 #ifndef __LINUX_OVERFLOW_H
3 #define __LINUX_OVERFLOW_H
5 #include <linux/compiler.h>
6 #include <linux/limits.h>
9 * We need to compute the minimum and maximum values representable in a given
10 * type. These macros may also be useful elsewhere. It would seem more obvious
11 * to do something like:
13 * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
14 * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
16 * Unfortunately, the middle expressions, strictly speaking, have
17 * undefined behaviour, and at least some versions of gcc warn about
18 * the type_max expression (but not if -fsanitize=undefined is in
19 * effect; in that case, the warning is deferred to runtime...).
21 * The slightly excessive casting in type_min is to make sure the
22 * macros also produce sensible values for the exotic type _Bool. [The
23 * overflow checkers only almost work for _Bool, but that's
24 * a-feature-not-a-bug, since people shouldn't be doing arithmetic on
25 * _Bools. Besides, the gcc builtins don't allow _Bool* as third
29 * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
30 * credit to Christian Biere.
32 #define is_signed_type(type) (((type)(-1)) < (type)1)
33 #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
34 #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
35 #define type_min(T) ((T)((T)-type_max(T)-(T)1))
38 * Avoids triggering -Wtype-limits compilation warning,
39 * while using unsigned data types to check a < 0.
41 #define is_non_negative(a) ((a) > 0 || (a) == 0)
42 #define is_negative(a) (!(is_non_negative(a)))
45 * Allows for effectively applying __must_check to a macro so we can have
46 * both the type-agnostic benefits of the macros while also being able to
47 * enforce that the return value is, in fact, checked.
49 static inline bool __must_check __must_check_overflow(bool overflow)
51 return unlikely(overflow);
55 * For simplicity and code hygiene, the fallback code below insists on
56 * a, b and *d having the same type (similar to the min() and max()
57 * macros), whereas gcc's type-generic overflow checkers accept
58 * different types. Hence we don't just make check_add_overflow an
59 * alias for __builtin_add_overflow, but add type checks similar to
62 #define check_add_overflow(a, b, d) __must_check_overflow(({ \
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_add_overflow(__a, __b, __d); \
71 #define check_sub_overflow(a, b, d) __must_check_overflow(({ \
72 typeof(a) __a = (a); \
73 typeof(b) __b = (b); \
74 typeof(d) __d = (d); \
75 (void) (&__a == &__b); \
76 (void) (&__a == __d); \
77 __builtin_sub_overflow(__a, __b, __d); \
80 #define check_mul_overflow(a, b, d) __must_check_overflow(({ \
81 typeof(a) __a = (a); \
82 typeof(b) __b = (b); \
83 typeof(d) __d = (d); \
84 (void) (&__a == &__b); \
85 (void) (&__a == __d); \
86 __builtin_mul_overflow(__a, __b, __d); \
89 /** check_shl_overflow() - Calculate a left-shifted value and check overflow
91 * @a: Value to be shifted
92 * @s: How many bits left to shift
93 * @d: Pointer to where to store the result
95 * Computes *@d = (@a << @s)
97 * Returns true if '*d' cannot hold the result or when 'a << s' doesn't
98 * make sense. Example conditions:
99 * - 'a << s' causes bits to be lost when stored in *d.
100 * - 's' is garbage (e.g. negative) or so large that the result of
101 * 'a << s' is guaranteed to be 0.
103 * - 'a << s' sets the sign bit, if any, in '*d'.
105 * '*d' will hold the results of the attempted shift, but is not
106 * considered "safe for use" if true is returned.
108 #define check_shl_overflow(a, s, d) __must_check_overflow(({ \
113 unsigned int _to_shift = \
114 is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \
115 *_d = (_a_full << _to_shift); \
116 (_to_shift != _s || is_negative(*_d) || is_negative(_a) || \
117 (*_d >> _to_shift) != _a); \
121 * array_size() - Calculate size of 2-dimensional array.
126 * Calculates size of 2-dimensional array: @a * @b.
128 * Returns: number of bytes needed to represent the array or SIZE_MAX on
131 static inline __must_check size_t array_size(size_t a, size_t b)
135 if (check_mul_overflow(a, b, &bytes))
142 * array3_size() - Calculate size of 3-dimensional array.
146 * @c: dimension three
148 * Calculates size of 3-dimensional array: @a * @b * @c.
150 * Returns: number of bytes needed to represent the array or SIZE_MAX on
153 static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
157 if (check_mul_overflow(a, b, &bytes))
159 if (check_mul_overflow(bytes, c, &bytes))
166 * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
167 * struct_size() below.
169 static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
173 if (check_mul_overflow(a, b, &bytes))
175 if (check_add_overflow(bytes, c, &bytes))
182 * struct_size() - Calculate size of structure with trailing array.
183 * @p: Pointer to the structure.
184 * @member: Name of the array member.
185 * @count: Number of elements in the array.
187 * Calculates size of memory needed for structure @p followed by an
188 * array of @count number of @member elements.
190 * Return: number of bytes needed or SIZE_MAX on overflow.
192 #define struct_size(p, member, count) \
194 sizeof(*(p)->member) + __must_be_array((p)->member),\
198 * flex_array_size() - Calculate size of a flexible array member
199 * within an enclosing structure.
201 * @p: Pointer to the structure.
202 * @member: Name of the flexible array member.
203 * @count: Number of elements in the array.
205 * Calculates size of a flexible array of @count number of @member
206 * elements, at the end of structure @p.
208 * Return: number of bytes needed or SIZE_MAX on overflow.
210 #define flex_array_size(p, member, count) \
212 sizeof(*(p)->member) + __must_be_array((p)->member))
214 #endif /* __LINUX_OVERFLOW_H */