hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/include/linux/overflow.h
....@@ -44,6 +44,16 @@
4444 #define is_non_negative(a) ((a) > 0 || (a) == 0)
4545 #define is_negative(a) (!(is_non_negative(a)))
4646
47
+/*
48
+ * Allows for effectively applying __must_check to a macro so we can have
49
+ * both the type-agnostic benefits of the macros while also being able to
50
+ * enforce that the return value is, in fact, checked.
51
+ */
52
+static inline bool __must_check __must_check_overflow(bool overflow)
53
+{
54
+ return unlikely(overflow);
55
+}
56
+
4757 #ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
4858 /*
4959 * For simplicity and code hygiene, the fallback code below insists on
....@@ -53,32 +63,32 @@
5363 * alias for __builtin_add_overflow, but add type checks similar to
5464 * below.
5565 */
56
-#define check_add_overflow(a, b, d) ({ \
66
+#define check_add_overflow(a, b, d) __must_check_overflow(({ \
5767 typeof(a) __a = (a); \
5868 typeof(b) __b = (b); \
5969 typeof(d) __d = (d); \
6070 (void) (&__a == &__b); \
6171 (void) (&__a == __d); \
6272 __builtin_add_overflow(__a, __b, __d); \
63
-})
73
+}))
6474
65
-#define check_sub_overflow(a, b, d) ({ \
75
+#define check_sub_overflow(a, b, d) __must_check_overflow(({ \
6676 typeof(a) __a = (a); \
6777 typeof(b) __b = (b); \
6878 typeof(d) __d = (d); \
6979 (void) (&__a == &__b); \
7080 (void) (&__a == __d); \
7181 __builtin_sub_overflow(__a, __b, __d); \
72
-})
82
+}))
7383
74
-#define check_mul_overflow(a, b, d) ({ \
84
+#define check_mul_overflow(a, b, d) __must_check_overflow(({ \
7585 typeof(a) __a = (a); \
7686 typeof(b) __b = (b); \
7787 typeof(d) __d = (d); \
7888 (void) (&__a == &__b); \
7989 (void) (&__a == __d); \
8090 __builtin_mul_overflow(__a, __b, __d); \
81
-})
91
+}))
8292
8393 #else
8494
....@@ -191,21 +201,20 @@
191201 })
192202
193203
194
-#define check_add_overflow(a, b, d) \
204
+#define check_add_overflow(a, b, d) __must_check_overflow( \
195205 __builtin_choose_expr(is_signed_type(typeof(a)), \
196206 __signed_add_overflow(a, b, d), \
197
- __unsigned_add_overflow(a, b, d))
207
+ __unsigned_add_overflow(a, b, d)))
198208
199
-#define check_sub_overflow(a, b, d) \
209
+#define check_sub_overflow(a, b, d) __must_check_overflow( \
200210 __builtin_choose_expr(is_signed_type(typeof(a)), \
201211 __signed_sub_overflow(a, b, d), \
202
- __unsigned_sub_overflow(a, b, d))
212
+ __unsigned_sub_overflow(a, b, d)))
203213
204
-#define check_mul_overflow(a, b, d) \
214
+#define check_mul_overflow(a, b, d) __must_check_overflow( \
205215 __builtin_choose_expr(is_signed_type(typeof(a)), \
206216 __signed_mul_overflow(a, b, d), \
207
- __unsigned_mul_overflow(a, b, d))
208
-
217
+ __unsigned_mul_overflow(a, b, d)))
209218
210219 #endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
211220
....@@ -228,7 +237,7 @@
228237 * '*d' will hold the results of the attempted shift, but is not
229238 * considered "safe for use" if false is returned.
230239 */
231
-#define check_shl_overflow(a, s, d) ({ \
240
+#define check_shl_overflow(a, s, d) __must_check_overflow(({ \
232241 typeof(a) _a = a; \
233242 typeof(s) _s = s; \
234243 typeof(d) _d = d; \
....@@ -238,7 +247,7 @@
238247 *_d = (_a_full << _to_shift); \
239248 (_to_shift != _s || is_negative(*_d) || is_negative(_a) || \
240249 (*_d >> _to_shift) != _a); \
241
-})
250
+}))
242251
243252 /**
244253 * array_size() - Calculate size of 2-dimensional array.
....@@ -285,11 +294,15 @@
285294 return bytes;
286295 }
287296
288
-static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
297
+/*
298
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
299
+ * struct_size() below.
300
+ */
301
+static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
289302 {
290303 size_t bytes;
291304
292
- if (check_mul_overflow(n, size, &bytes))
305
+ if (check_mul_overflow(a, b, &bytes))
293306 return SIZE_MAX;
294307 if (check_add_overflow(bytes, c, &bytes))
295308 return SIZE_MAX;
....@@ -301,16 +314,33 @@
301314 * struct_size() - Calculate size of structure with trailing array.
302315 * @p: Pointer to the structure.
303316 * @member: Name of the array member.
304
- * @n: Number of elements in the array.
317
+ * @count: Number of elements in the array.
305318 *
306319 * Calculates size of memory needed for structure @p followed by an
307
- * array of @n @member elements.
320
+ * array of @count number of @member elements.
308321 *
309322 * Return: number of bytes needed or SIZE_MAX on overflow.
310323 */
311
-#define struct_size(p, member, n) \
312
- __ab_c_size(n, \
324
+#define struct_size(p, member, count) \
325
+ __ab_c_size(count, \
313326 sizeof(*(p)->member) + __must_be_array((p)->member),\
314327 sizeof(*(p)))
315328
329
+/**
330
+ * flex_array_size() - Calculate size of a flexible array member
331
+ * within an enclosing structure.
332
+ *
333
+ * @p: Pointer to the structure.
334
+ * @member: Name of the flexible array member.
335
+ * @count: Number of elements in the array.
336
+ *
337
+ * Calculates size of a flexible array of @count number of @member
338
+ * elements, at the end of structure @p.
339
+ *
340
+ * Return: number of bytes needed or SIZE_MAX on overflow.
341
+ */
342
+#define flex_array_size(p, member, count) \
343
+ array_size(count, \
344
+ sizeof(*(p)->member) + __must_be_array((p)->member))
345
+
316346 #endif /* __LINUX_OVERFLOW_H */