[next,v2,5/5] minmax: Relax check to allow comparison between int and small unsigned constants.

Message ID b4ce9dad748e489f9314a2dc95615033@AcuMS.aculab.com
State New
Headers
Series minmax: Relax type checks in min() and max(). |

Commit Message

David Laight July 28, 2023, 2:53 p.m. UTC
  Convert constants between 0 and INT_MAX to 'int' prior to comparisons
so that min(signed_var, 20u) and, more commonly, min(signed_var, sizeof())
are both valid.

Signed-off-by: David Laight <david.laight@aculab.com>
---
v2: Add cast to fix min/max with pointer types.

 include/linux/minmax.h | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
  

Comments

kernel test robot July 28, 2023, 9:43 p.m. UTC | #1
Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master crng-random/master v6.5-rc3 next-20230728]
[cannot apply to next-20230728]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Laight/minmax-Add-min_unsigned-a-b-and-max_unsigned-a-b/20230728-225439
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/b4ce9dad748e489f9314a2dc95615033%40AcuMS.aculab.com
patch subject: [PATCH next v2 5/5] minmax: Relax check to allow comparison between int and small unsigned constants.
config: alpha-randconfig-r024-20230727 (https://download.01.org/0day-ci/archive/20230729/202307290538.EtRKfGgC-lkp@intel.com/config)
compiler: alpha-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230729/202307290538.EtRKfGgC-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202307290538.EtRKfGgC-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from include/linux/kernel.h:27,
                    from include/linux/cpumask.h:10,
                    from include/linux/mm_types_task.h:14,
                    from include/linux/mm_types.h:5,
                    from include/linux/buildid.h:5,
                    from include/linux/module.h:14,
                    from net/ceph/osdmap.c:5:
   net/ceph/osdmap.c: In function 'osdmap_decode':
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:27: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                           ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:27: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:45: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                             ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:43: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:51: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                                   ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:27: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:57: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                                         ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:43: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:42:16: note: in expansion of macro '__int_const'
      42 |         typeof(__int_const(x)) unique_x = (x);          \
         |                ^~~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:43:16: note: in expansion of macro '__int_const'
      43 |         typeof(__int_const(y)) unique_y = (y);          \
         |                ^~~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
   In file included from include/linux/container_of.h:5,
                    from include/linux/list.h:5,
                    from include/linux/module.h:12:
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/minmax.h:44:9: note: in expansion of macro 'static_assert'
      44 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:34:17: note: in expansion of macro '__is_noneg_int'
      34 |                 __is_noneg_int(x) || __is_noneg_int(y))
         |                 ^~~~~~~~~~~~~~
   include/linux/minmax.h:44:23: note: in expansion of macro '__types_ok'
      44 |         static_assert(__types_ok(x, y),                 \
         |                       ^~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/minmax.h:44:9: note: in expansion of macro 'static_assert'
      44 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:34:38: note: in expansion of macro '__is_noneg_int'
      34 |                 __is_noneg_int(x) || __is_noneg_int(y))
         |                                      ^~~~~~~~~~~~~~
   include/linux/minmax.h:44:23: note: in expansion of macro '__types_ok'
      44 |         static_assert(__types_ok(x, y),                 \
         |                       ^~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:1773:54: note: in expansion of macro 'min'
    1773 |         err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
         |                                                      ^~~
   net/ceph/osdmap.c: In function 'osdmap_apply_incremental':
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:27: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                           ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:27: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:45: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                             ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:43: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:51: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                                   ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:27: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:57: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                                         ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:43: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:42:16: note: in expansion of macro '__int_const'
      42 |         typeof(__int_const(x)) unique_x = (x);          \
         |                ^~~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:43:16: note: in expansion of macro '__int_const'
      43 |         typeof(__int_const(y)) unique_y = (y);          \
         |                ^~~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/minmax.h:44:9: note: in expansion of macro 'static_assert'
      44 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:34:17: note: in expansion of macro '__is_noneg_int'
      34 |                 __is_noneg_int(x) || __is_noneg_int(y))
         |                 ^~~~~~~~~~~~~~
   include/linux/minmax.h:44:23: note: in expansion of macro '__types_ok'
      44 |         static_assert(__types_ok(x, y),                 \
         |                       ^~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/minmax.h:44:9: note: in expansion of macro 'static_assert'
      44 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:34:38: note: in expansion of macro '__is_noneg_int'
      34 |                 __is_noneg_int(x) || __is_noneg_int(y))
         |                                      ^~~~~~~~~~~~~~
   include/linux/minmax.h:44:23: note: in expansion of macro '__types_ok'
      44 |         static_assert(__types_ok(x, y),                 \
         |                       ^~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2000:46: note: in expansion of macro 'min'
    2000 |                 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
         |                                              ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:27: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                           ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:27: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
         |                                                         ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:45: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                             ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:43: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
         |                                                         ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:51: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                                   ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:27: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
         |                                                         ^~~
>> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:39:57: note: in definition of macro '__cmp'
      39 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
         |                                                         ^
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:50:43: note: in expansion of macro '__int_const'
      50 |                 __cmp(op, __int_const(x), __int_const(y)),      \
         |                                           ^~~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
         |                                                         ^~~
   include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:42:16: note: in expansion of macro '__int_const'
      42 |         typeof(__int_const(x)) unique_x = (x);          \
         |                ^~~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
         |                                                         ^~~
   include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/minmax.h:28:47: note: in expansion of macro '__is_noneg_int'
      28 | #define __int_const(x)  __builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
         |                                               ^~~~~~~~~~~~~~
   include/linux/minmax.h:43:16: note: in expansion of macro '__int_const'
      43 |         typeof(__int_const(y)) unique_y = (y);          \
         |                ^~~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
         |                                                         ^~~
   include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/minmax.h:44:9: note: in expansion of macro 'static_assert'
      44 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:34:17: note: in expansion of macro '__is_noneg_int'
      34 |                 __is_noneg_int(x) || __is_noneg_int(y))
         |                 ^~~~~~~~~~~~~~
   include/linux/minmax.h:44:23: note: in expansion of macro '__types_ok'
      44 |         static_assert(__types_ok(x, y),                 \
         |                       ^~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
         |                                                         ^~~
   include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
      26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
         |                      ^~
   include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
      78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
         |                                                        ^~~~
   include/linux/minmax.h:44:9: note: in expansion of macro 'static_assert'
      44 |         static_assert(__types_ok(x, y),                 \
         |         ^~~~~~~~~~~~~
   include/linux/minmax.h:34:38: note: in expansion of macro '__is_noneg_int'
      34 |                 __is_noneg_int(x) || __is_noneg_int(y))
         |                                      ^~~~~~~~~~~~~~
   include/linux/minmax.h:44:23: note: in expansion of macro '__types_ok'
      44 |         static_assert(__types_ok(x, y),                 \
         |                       ^~~~~~~~~~
   include/linux/minmax.h:51:17: note: in expansion of macro '__cmp_once'
      51 |                 __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
         |                 ^~~~~~~~~~
   include/linux/minmax.h:78:25: note: in expansion of macro '__careful_cmp'
      78 | #define min(x, y)       __careful_cmp(min, x, y)
         |                         ^~~~~~~~~~~~~
   net/ceph/osdmap.c:2007:57: note: in expansion of macro 'min'
    2007 |                                        crush_decode(*p, min(*p + len, end)));
..


vim +26 include/linux/minmax.h

     7	
     8	/*
     9	 * min()/max()/clamp() macros must accomplish three things:
    10	 *
    11	 * - Avoid multiple evaluations of the arguments (so side-effects like
    12	 *   "x++" happen only once) when non-constant.
    13	 * - Perform signed v unsigned type-checking (to generate compile
    14	 *   errors instead of nasty runtime surprises).
    15	 *   Constants from 0 to INT_MAX are cast to (int) so can be used
    16	 *   in comparisons with signed types.
    17	 * - Retain result as a constant expressions when called with only
    18	 *   constant expressions (to avoid tripping VLA warnings in stack
    19	 *   allocation usage).
    20	 */
    21	#define __typecheck(x, y) \
    22		(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
    23	
    24	#define __is_noneg_int(x)					\
    25		__builtin_choose_expr(!__is_constexpr(x), false, 	\
  > 26			((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
    27
  
David Laight July 30, 2023, 4:55 p.m. UTC | #2
From: kernel test robot
> Sent: 28 July 2023 22:43
> 
> kernel test robot noticed the following build warnings:
> 
> [auto build test WARNING on akpm-mm/mm-everything]
> [also build test WARNING on linus/master crng-random/master v6.5-rc3 next-20230728]
> [cannot apply to next-20230728]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/David-Laight/minmax-Add-min_unsigned-a-b-and-
> max_unsigned-a-b/20230728-225439
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
> patch link:    https://lore.kernel.org/r/b4ce9dad748e489f9314a2dc95615033%40AcuMS.aculab.com
> patch subject: [PATCH next v2 5/5] minmax: Relax check to allow comparison between int and small
> unsigned constants.
> config: alpha-randconfig-r024-20230727 (https://download.01.org/0day-
> ci/archive/20230729/202307290538.EtRKfGgC-lkp@intel.com/config)
> compiler: alpha-linux-gcc (GCC) 12.3.0
> reproduce: (https://download.01.org/0day-ci/archive/20230729/202307290538.EtRKfGgC-
> lkp@intel.com/reproduce)
> 
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202307290538.EtRKfGgC-lkp@intel.com/
> 
> All warnings (new ones prefixed by >>):
> 
>    In file included from include/linux/kernel.h:27,
>                     from include/linux/cpumask.h:10,
>                     from include/linux/mm_types_task.h:14,
>                     from include/linux/mm_types.h:5,
>                     from include/linux/buildid.h:5,
>                     from include/linux/module.h:14,
>                     from net/ceph/osdmap.c:5:
>    net/ceph/osdmap.c: In function 'osdmap_decode':
> >> include/linux/minmax.h:26:22: warning: ordered comparison of pointer with null pointer [-Wextra]
>       26 |                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))

I have a cunning plan to fix this (and warnings about testing 'unsigned >= 0').
Basically replace the first test with:
	__builtin_choose_expr(is_signed(x), x, 1) >= 0

(Also fixes the build with clang < 16.)

Hopefully that v3 will keep the build robert happy :-)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  
David Laight July 31, 2023, 1:26 p.m. UTC | #3
By the time I've done:

#define __is_noneg_int(x)					\
	__builtin_choose_expr(!__is_constexpr(x), false, 	\
		((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))

#define __is_signed(x)                                                         \
       __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))),        \
               is_signed_type(typeof(x)), 0)

#define __types_ok(x, y) 					\
	(__is_signed(x) == __is_signed(y) ||			\
		__is_signed((x) + 0) == __is_signed((y) + 0) ||	\
		__is_noneg_int(x) || __is_noneg_int(y))

the error message for

>  	static_assert(__types_ok(x, y),			\
>  		#op "(" #x ", " #y ") signedness error, fix types or consider " #op "_unsigned() before " #op "_t()"); \

generated by clang 8.0.0 and later is similar to (see https://godbolt.org/z/jq613Gnsa):

<source>:49:12: error: static assertion failed due to requirement '__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0) == __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((unsigned int)(-1)) < (unsigned int)1)) * 0L)) : (int *)8))), (((unsigned int)(-1)) < (unsigned int)1), 0) || __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0) == __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((unsigned int)(-1)) < (unsigned int)1)) * 0L)) : (int *)8))), (((unsigned int)(-1)) < (unsigned int)1), 0) || (__builtin_choose_expr(!(sizeof(int) == sizeof (*(8 ? ((void *)((long)(a) * 0L)) : (int *)8))), 0, __builtin_choose_expr(__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0), a, 0) >= 0 && (a) <= (int)(long)2147483647)) || (__builtin_choose_expr(!(sizeof(int) == sizeof (*(8 ? ((void *)((long)(2147483648U - 0) * 0L)) : (int *)8))), 0, __builtin_choose_expr(__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((unsigned int)(-1)) < (unsigned int)1)) * 0L)) : (int *)8))), (((unsigned int)(-1)) < (unsigned int)1), 0), 2147483648U - 0, 0) >= 0 && (2147483648U - 0) <= (unsigned int)(long)2147483647))': min(a, 0x80000000u - 0) signedness error, fix types or consider min_unsigned() before min_t()

Repeating the expression seems somewhat sub-optimal!
Surely it shouldn't be outputting the expansion of the
input when an error message is supplied?

Is there any (sane) way to stop it being that verbose?

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  
Nick Desaulniers Aug. 1, 2023, 4:02 p.m. UTC | #4
On Mon, Jul 31, 2023 at 6:27 AM David Laight <David.Laight@aculab.com> wrote:
>
> By the time I've done:
>
> #define __is_noneg_int(x)                                       \
>         __builtin_choose_expr(!__is_constexpr(x), false,        \
>                 ((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
>
> #define __is_signed(x)                                                         \
>        __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))),        \
>                is_signed_type(typeof(x)), 0)
>
> #define __types_ok(x, y)                                        \
>         (__is_signed(x) == __is_signed(y) ||                    \
>                 __is_signed((x) + 0) == __is_signed((y) + 0) || \
>                 __is_noneg_int(x) || __is_noneg_int(y))
>
> the error message for
>
> >       static_assert(__types_ok(x, y),                 \
> >               #op "(" #x ", " #y ") signedness error, fix types or consider " #op "_unsigned() before " #op "_t()"); \
>
> generated by clang 8.0.0 and later is similar to (see https://godbolt.org/z/jq613Gnsa):
>
> <source>:49:12: error: static assertion failed due to requirement '__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0) == __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((unsigned int)(-1)) < (unsigned int)1)) * 0L)) : (int *)8))), (((unsigned int)(-1)) < (unsigned int)1), 0) || __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0) == __builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((unsigned int)(-1)) < (unsigned int)1)) * 0L)) : (int *)8))), (((unsigned int)(-1)) < (unsigned int)1), 0) || (__builtin_choose_expr(!(sizeof(int) == sizeof (*(8 ? ((void *)((long)(a) * 0L)) : (int *)8))), 0, __builtin_choose_expr(__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((int)(-1)) < (int)1)) * 0L)) : (int *)8))), (((int)(-1)) < (int)1), 0), a, 0) >= 0 && (a) <= (int)(long)2147483647)) || (__builtin_choose_expr(!(sizeof(int) == sizeof (*(8 ? ((void *)((long)(2147483648U - 0) * 0L)) : (int *)8))), 0, __builtin_choose_expr(__builtin_choose_expr((sizeof(int) == sizeof (*(8 ? ((void *)((long)((((unsigned int)(-1)) < (unsigned int)1)) * 0L)) : (int *)8))), (((unsigned int)(-1)) < (unsigned int)1), 0), 2147483648U - 0, 0) >= 0 && (2147483648U - 0) <= (unsigned int)(long)2147483647))': min(a, 0x80000000u - 0) signedness error, fix types or consider min_unsigned() before min_t()
>
> Repeating the expression seems somewhat sub-optimal!
> Surely it shouldn't be outputting the expansion of the
> input when an error message is supplied?
>
> Is there any (sane) way to stop it being that verbose?

No, but we can probably change that in clang. Filed:
https://github.com/llvm/llvm-project/issues/64310

>
>         David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
  

Patch

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 7d3ad9cbbff6..28edafb3dcca 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -8,20 +8,30 @@ 
 /*
  * min()/max()/clamp() macros must accomplish three things:
  *
- * - avoid multiple evaluations of the arguments (so side-effects like
+ * - Avoid multiple evaluations of the arguments (so side-effects like
  *   "x++" happen only once) when non-constant.
- * - perform signed v unsigned type-checking (to generate compile
+ * - Perform signed v unsigned type-checking (to generate compile
  *   errors instead of nasty runtime surprises).
- * - retain result as a constant expressions when called with only
+ *   Constants from 0 to INT_MAX are cast to (int) so can be used
+ *   in comparisons with signed types.
+ * - Retain result as a constant expressions when called with only
  *   constant expressions (to avoid tripping VLA warnings in stack
  *   allocation usage).
  */
 #define __typecheck(x, y) \
 	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
 
-#define __types_ok(x, y) \
-	(is_signed_type(typeof(x)) == is_signed_type(typeof(y)) ||	\
-		is_signed_type(typeof((x) + 0)) == is_signed_type(typeof((y) + 0)))
+#define __is_noneg_int(x)					\
+	__builtin_choose_expr(!__is_constexpr(x), false, 	\
+		((x) >= (typeof(x))0 && (x) <= (typeof((x) + 0))(long)__INT_MAX__))
+
+#define __int_const(x)	__builtin_choose_expr(__is_noneg_int(x), (int)(long)(x), (x))
+
+#define __is_signed(x)	is_signed_type(typeof(x))
+#define __types_ok(x, y) 					\
+	(__is_signed(x) == __is_signed(y) ||			\
+		__is_signed((x) + 0) == __is_signed((y) + 0) ||	\
+		__is_noneg_int(x) || __is_noneg_int(y))
 
 #define __cmp_op_min <
 #define __cmp_op_max >
@@ -29,24 +39,24 @@ 
 #define __cmp(op, x, y)	((x) __cmp_op_##op (y) ? (x) : (y))
 
 #define __cmp_once(op, x, y, unique_x, unique_y) ({	\
-	typeof(x) unique_x = (x);			\
-	typeof(y) unique_y = (y);			\
+	typeof(__int_const(x)) unique_x = (x);		\
+	typeof(__int_const(y)) unique_y = (y);		\
 	static_assert(__types_ok(x, y),			\
 		#op "(" #x ", " #y ") signedness error, fix types or consider " #op "_unsigned() before " #op "_t()"); \
 	__cmp(op, unique_x, unique_y); })
 
 #define __careful_cmp(op, x, y)					\
 	__builtin_choose_expr(__is_constexpr((x) - (y)),	\
-		__cmp(op, x, y),				\
+		__cmp(op, __int_const(x), __int_const(y)),	\
 		__cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
 
 #define __clamp(val, lo, hi)	\
 	((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
 
 #define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({		\
-	typeof(val) unique_val = (val);						\
-	typeof(lo) unique_lo = (lo);						\
-	typeof(hi) unique_hi = (hi);						\
+	typeof(__int_const(val)) unique_val = (val);				\
+	typeof(__int_const(lo)) unique_lo = (lo);				\
+	typeof(__int_const(hi)) unique_hi = (hi);				\
 	static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), 	\
 			(lo) <= (hi), true),					\
 		"clamp() low limit " #lo " greater than high limit " #hi);	\