Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
73 * @factor1: first factor
74 * @factor2: second factor
75 *
76 * Returns: calculate @factor1 * @factor2, both promoted to size_t,
77 * with any overflow causing the return value to be SIZE_MAX. The
78 * lvalue must be size_t to avoid implicit type conversion.
79 */
80static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
81{
82 size_t bytes;
83
84 if (check_mul_overflow(factor1, factor2, &bytes))
85 return SIZE_MAX;
86
87 return bytes;
88}
89
90/**
91 * array_size() - Calculate size of 2-dimensional array.
92 *
93 * @a: dimension one
94 * @b: dimension two
95 *
96 * Calculates size of 2-dimensional array: @a * @b.
97 *
98 * Returns: number of bytes needed to represent the array or SIZE_MAX on
99 * overflow.
100 */
101static inline __must_check size_t array_size(size_t a, size_t b)
102{
103 size_t bytes;
104
105 if (check_mul_overflow(a, b, &bytes))
106 return SIZE_MAX;
107
108 return bytes;
109}
110
111/**
112 * array3_size() - Calculate size of 3-dimensional array.
113 *
114 * @a: dimension one
115 * @b: dimension two
116 * @c: dimension three
117 *
118 * Calculates size of 3-dimensional array: @a * @b * @c.
119 *
120 * Returns: number of bytes needed to represent the array or SIZE_MAX on
121 * overflow.
122 */
123static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
124{
125 size_t bytes;
126
127 if (check_mul_overflow(a, b, &bytes))
128 return SIZE_MAX;
129 if (check_mul_overflow(bytes, c, &bytes))
130 return SIZE_MAX;
131
132 return bytes;
133}
134
135static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
136{
137 size_t bytes;
138
139 if (check_mul_overflow(n, size, &bytes))
140 return SIZE_MAX;
141 if (check_add_overflow(bytes, c, &bytes))
142 return SIZE_MAX;
143
144 return bytes;
145}
146
147/**
148 * struct_size() - Calculate size of structure with trailing array.
149 * @p: Pointer to the structure.
150 * @member: Name of the array member.
151 * @n: Number of elements in the array.
152 *
153 * Calculates size of memory needed for structure @p followed by an
154 * array of @n @member elements.
155 *
156 * Return: number of bytes needed or SIZE_MAX on overflow.
157 */
158#define struct_size(p, member, n) \
159 __ab_c_size(n, \
160 sizeof(*(p)->member) + __must_be_array((p)->member),\
161 sizeof(*(p)))
162
163#endif /* __LINUX_OVERFLOW_H */