Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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/fe7e6c542e094bfca655abcd323c1c98@AcuMS.aculab.com
Signed-off-by: David Laight <david.laight@aculab.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Laight and committed by
Andrew Morton
d03eba99 80fcac55

+32 -28
+32 -28
include/linux/minmax.h
··· 12 12 * 13 13 * - avoid multiple evaluations of the arguments (so side-effects like 14 14 * "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). 18 17 * - retain result as a constant expressions when called with only 19 18 * constant expressions (to avoid tripping VLA warnings in stack 20 19 * allocation usage). ··· 21 22 #define __typecheck(x, y) \ 22 23 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) 23 24 24 - #define __no_side_effects(x, y) \ 25 - (__is_constexpr(x) && __is_constexpr(y)) 25 + /* is_signed_type() isn't a constexpr for pointer types */ 26 + #define __is_signed(x) \ 27 + __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ 28 + is_signed_type(typeof(x)), 0) 26 29 27 - #define __safe_cmp(x, y) \ 28 - (__typecheck(x, y) && __no_side_effects(x, y)) 30 + #define __types_ok(x, y) \ 31 + (__is_signed(x) == __is_signed(y)) 29 32 30 - #define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) 33 + #define __cmp_op_min < 34 + #define __cmp_op_max > 31 35 32 - #define __cmp_once(x, y, unique_x, unique_y, op) ({ \ 36 + #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y)) 37 + 38 + #define __cmp_once(op, x, y, unique_x, unique_y) ({ \ 33 39 typeof(x) unique_x = (x); \ 34 40 typeof(y) unique_y = (y); \ 35 - __cmp(unique_x, unique_y, op); }) 41 + static_assert(__types_ok(x, y), \ 42 + #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \ 43 + __cmp(op, unique_x, unique_y); }) 36 44 37 - #define __careful_cmp(x, y, op) \ 38 - __builtin_choose_expr(__safe_cmp(x, y), \ 39 - __cmp(x, y, op), \ 40 - __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) 45 + #define __careful_cmp(op, x, y) \ 46 + __builtin_choose_expr(__is_constexpr((x) - (y)), \ 47 + __cmp(op, x, y), \ 48 + __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) 41 49 42 50 #define __clamp(val, lo, hi) \ 43 51 ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) ··· 53 47 typeof(val) unique_val = (val); \ 54 48 typeof(lo) unique_lo = (lo); \ 55 49 typeof(hi) unique_hi = (hi); \ 50 + static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ 51 + (lo) <= (hi), true), \ 52 + "clamp() low limit " #lo " greater than high limit " #hi); \ 53 + static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ 54 + static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ 56 55 __clamp(unique_val, unique_lo, unique_hi); }) 57 56 58 - #define __clamp_input_check(lo, hi) \ 59 - (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ 60 - __is_constexpr((lo) > (hi)), (lo) > (hi), false))) 61 - 62 57 #define __careful_clamp(val, lo, hi) ({ \ 63 - __clamp_input_check(lo, hi) + \ 64 - __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \ 65 - __typecheck(hi, lo) && __is_constexpr(val) && \ 66 - __is_constexpr(lo) && __is_constexpr(hi), \ 58 + __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ 67 59 __clamp(val, lo, hi), \ 68 60 __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ 69 61 __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) ··· 71 67 * @x: first value 72 68 * @y: second value 73 69 */ 74 - #define min(x, y) __careful_cmp(x, y, <) 70 + #define min(x, y) __careful_cmp(min, x, y) 75 71 76 72 /** 77 73 * max - return maximum of two values of the same or compatible types 78 74 * @x: first value 79 75 * @y: second value 80 76 */ 81 - #define max(x, y) __careful_cmp(x, y, >) 77 + #define max(x, y) __careful_cmp(max, x, y) 82 78 83 79 /** 84 80 * umin - return minimum of two non-negative values ··· 87 83 * @y: second value 88 84 */ 89 85 #define umin(x, y) \ 90 - __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) 86 + __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) 91 87 92 88 /** 93 89 * umax - return maximum of two non-negative values ··· 95 91 * @y: second value 96 92 */ 97 93 #define umax(x, y) \ 98 - __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, >) 94 + __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) 99 95 100 96 /** 101 97 * min3 - return minimum of three values ··· 147 143 * @x: first value 148 144 * @y: second value 149 145 */ 150 - #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <) 146 + #define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) 151 147 152 148 /** 153 149 * max_t - return maximum of two values, using the specified type ··· 155 151 * @x: first value 156 152 * @y: second value 157 153 */ 158 - #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >) 154 + #define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) 159 155 160 156 /* 161 157 * Do not check the array parameter using __must_be_array().