minmax: allow min()/max()/clamp() if the arguments have the same signedness.
authorDavid Laight <David.Laight@ACULAB.COM>
Mon, 18 Sep 2023 08:17:15 +0000 (08:17 +0000)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 18 Oct 2023 21:43:22 +0000 (14:43 -0700)
commitd03eba99f5bf7cbc6e2fdde3b6fa36954ad58e09
tree39bb17b041664cbc3f8495b363ef07e2ee96ce3b
parent80fcac55385ccb710d33a20dc1caaef29bd5a921
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>
include/linux/minmax.h