Skip to content

Commit bd1ebf2

Browse files
committed
overflow: Allow non-type arg to type_max() and type_min()
A common use of type_max() is to find the max for the type of a variable. Using the pattern type_max(typeof(var)) is needlessly verbose. Instead, since typeof(type) == type we can just explicitly call typeof() on the argument to type_max() and type_min(). Add wrappers for readability. We can do some replacements right away: $ git grep '\btype_\(min\|max\)(typeof' | wc -l 11 Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent e606e4b commit bd1ebf2

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

include/linux/overflow.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
* credit to Christian Biere.
3232
*/
3333
#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))
34+
#define __type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
35+
#define type_max(t) __type_max(typeof(t))
36+
#define __type_min(T) ((T)((T)-type_max(T)-(T)1))
37+
#define type_min(t) __type_min(typeof(t))
3638

3739
/*
3840
* Avoids triggering -Wtype-limits compilation warning,
@@ -207,10 +209,10 @@ static inline bool __must_check __must_check_overflow(bool overflow)
207209

208210
#define __overflows_type_constexpr(x, T) ( \
209211
is_unsigned_type(typeof(x)) ? \
210-
(x) > type_max(typeof(T)) : \
212+
(x) > type_max(T) : \
211213
is_unsigned_type(typeof(T)) ? \
212-
(x) < 0 || (x) > type_max(typeof(T)) : \
213-
(x) < type_min(typeof(T)) || (x) > type_max(typeof(T)))
214+
(x) < 0 || (x) > type_max(T) : \
215+
(x) < type_min(T) || (x) > type_max(T))
214216

215217
#define __overflows_type(x, T) ({ \
216218
typeof(T) v = 0; \

0 commit comments

Comments
 (0)