Skip to content

Commit d03eba9

Browse files
david-laightakpm00
authored andcommitted
minmax: allow min()/max()/clamp() if the arguments have the same signedness.
The type-check in min()/max() is there to stop unexpected results if a negative value gets converted to a large unsigned value. However it also rejects 'unsigned int' v 'unsigned long' compares which are common and never problematc. Replace the 'same type' check with a 'same signedness' check. The new test isn't itself a compile time error, so use static_assert() to report the error and give a meaningful error message. Due to the way builtin_choose_expr() works detecting the error in the 'non-constant' side (where static_assert() can be used) also detects errors when the arguments are constant. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: David Laight <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Jason A. Donenfeld <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 80fcac5 commit d03eba9

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

include/linux/minmax.h

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,39 @@
1212
*
1313
* - avoid multiple evaluations of the arguments (so side-effects like
1414
* "x++" happen only once) when non-constant.
15-
* - perform strict type-checking (to generate warnings instead of
16-
* nasty runtime surprises). See the "unnecessary" pointer comparison
17-
* in __typecheck().
15+
* - perform signed v unsigned type-checking (to generate compile
16+
* errors instead of nasty runtime surprises).
1817
* - retain result as a constant expressions when called with only
1918
* constant expressions (to avoid tripping VLA warnings in stack
2019
* allocation usage).
2120
*/
2221
#define __typecheck(x, y) \
2322
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
2423

25-
#define __no_side_effects(x, y) \
26-
(__is_constexpr(x) && __is_constexpr(y))
24+
/* is_signed_type() isn't a constexpr for pointer types */
25+
#define __is_signed(x) \
26+
__builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \
27+
is_signed_type(typeof(x)), 0)
2728

28-
#define __safe_cmp(x, y) \
29-
(__typecheck(x, y) && __no_side_effects(x, y))
29+
#define __types_ok(x, y) \
30+
(__is_signed(x) == __is_signed(y))
3031

31-
#define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
32+
#define __cmp_op_min <
33+
#define __cmp_op_max >
3234

33-
#define __cmp_once(x, y, unique_x, unique_y, op) ({ \
35+
#define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
36+
37+
#define __cmp_once(op, x, y, unique_x, unique_y) ({ \
3438
typeof(x) unique_x = (x); \
3539
typeof(y) unique_y = (y); \
36-
__cmp(unique_x, unique_y, op); })
40+
static_assert(__types_ok(x, y), \
41+
#op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \
42+
__cmp(op, unique_x, unique_y); })
3743

38-
#define __careful_cmp(x, y, op) \
39-
__builtin_choose_expr(__safe_cmp(x, y), \
40-
__cmp(x, y, op), \
41-
__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
44+
#define __careful_cmp(op, x, y) \
45+
__builtin_choose_expr(__is_constexpr((x) - (y)), \
46+
__cmp(op, x, y), \
47+
__cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
4248

4349
#define __clamp(val, lo, hi) \
4450
((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
@@ -47,17 +53,15 @@
4753
typeof(val) unique_val = (val); \
4854
typeof(lo) unique_lo = (lo); \
4955
typeof(hi) unique_hi = (hi); \
56+
static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \
57+
(lo) <= (hi), true), \
58+
"clamp() low limit " #lo " greater than high limit " #hi); \
59+
static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \
60+
static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \
5061
__clamp(unique_val, unique_lo, unique_hi); })
5162

52-
#define __clamp_input_check(lo, hi) \
53-
(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
54-
__is_constexpr((lo) > (hi)), (lo) > (hi), false)))
55-
5663
#define __careful_clamp(val, lo, hi) ({ \
57-
__clamp_input_check(lo, hi) + \
58-
__builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \
59-
__typecheck(hi, lo) && __is_constexpr(val) && \
60-
__is_constexpr(lo) && __is_constexpr(hi), \
64+
__builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \
6165
__clamp(val, lo, hi), \
6266
__clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
6367
__UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
@@ -67,14 +71,14 @@
6771
* @x: first value
6872
* @y: second value
6973
*/
70-
#define min(x, y) __careful_cmp(x, y, <)
74+
#define min(x, y) __careful_cmp(min, x, y)
7175

7276
/**
7377
* max - return maximum of two values of the same or compatible types
7478
* @x: first value
7579
* @y: second value
7680
*/
77-
#define max(x, y) __careful_cmp(x, y, >)
81+
#define max(x, y) __careful_cmp(max, x, y)
7882

7983
/**
8084
* umin - return minimum of two non-negative values
@@ -83,15 +87,15 @@
8387
* @y: second value
8488
*/
8589
#define umin(x, y) \
86-
__careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <)
90+
__careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
8791

8892
/**
8993
* umax - return maximum of two non-negative values
9094
* @x: first value
9195
* @y: second value
9296
*/
9397
#define umax(x, y) \
94-
__careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, >)
98+
__careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
9599

96100
/**
97101
* min3 - return minimum of three values
@@ -143,15 +147,15 @@
143147
* @x: first value
144148
* @y: second value
145149
*/
146-
#define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
150+
#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y))
147151

148152
/**
149153
* max_t - return maximum of two values, using the specified type
150154
* @type: data type to use
151155
* @x: first value
152156
* @y: second value
153157
*/
154-
#define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
158+
#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y))
155159

156160
/*
157161
* Do not check the array parameter using __must_be_array().

0 commit comments

Comments
 (0)