lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251219-remove_wtype-limits-v2-4-2e92b3f566c5@kernel.org>
Date: Fri, 19 Dec 2025 23:39:48 +0100
From: Vincent Mailhol <mailhol@...nel.org>
To: Nathan Chancellor <nathan@...nel.org>, Nicolas Schier <nsc@...nel.org>, 
 Nick Desaulniers <nick.desaulniers+lkml@...il.com>, 
 Bill Wendling <morbo@...gle.com>, Justin Stitt <justinstitt@...gle.com>, 
 Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>, 
 Maxime Ripard <mripard@...nel.org>, Thomas Zimmermann <tzimmermann@...e.de>, 
 David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>, 
 Chris Mason <clm@...com>, David Sterba <dsterba@...e.com>, 
 Kees Cook <kees@...nel.org>, "Gustavo A. R. Silva" <gustavoars@...nel.org>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>, 
 linux-kbuild@...r.kernel.org, linux-sparse@...r.kernel.org, 
 linux-kernel@...r.kernel.org, llvm@...ts.linux.dev, 
 dri-devel@...ts.freedesktop.org, linux-btrfs@...r.kernel.org, 
 linux-hardening@...r.kernel.org, Vincent Mailhol <mailhol@...nel.org>
Subject: [PATCH v2 4/4] minmax: remove useless cast in __is_nonneg()

The function like macro __is_nonneg() casts its argument to (long long)
in an attempt to silence -Wtype-limits warnings on unsigned values.

But this workaround is incomplete as proven here:

  $ cat foo.c
  #include <linux/minmax.h>

  int foo(unsigned int a)
  {
  	return __is_nonneg(a);
  }
  $ make CFLAGS_KERNEL="-Wtype-limits" foo.o
    CALL    scripts/checksyscalls.sh
    DESCEND objtool
    INSTALL libsubcmd_headers
    CC      foo.o
  foo.c: In function 'foo':
  ./include/linux/minmax.h:68:57: warning: comparison is always true due to limited range of data type [-Wtype-limits]
     68 | #define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
        |                                                         ^~
  ./include/linux/compiler.h:350:50: note: in definition of macro 'statically_true'
    350 | #define statically_true(x) (__builtin_constant_p(x) && (x))
        |                                                  ^
  foo.c:5:16: note: in expansion of macro '__is_nonneg'
      5 |         return __is_nonneg(a);
        |                ^~~~~~~~~~~
  ./include/linux/minmax.h:68:57: warning: comparison is always true due to limited range of data type [-Wtype-limits]
     68 | #define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
        |                                                         ^~
  ./include/linux/compiler.h:350:57: note: in definition of macro 'statically_true'
    350 | #define statically_true(x) (__builtin_constant_p(x) && (x))
        |                                                         ^
  foo.c:5:16: note: in expansion of macro '__is_nonneg'
      5 |         return __is_nonneg(a);
        |                ^~~~~~~~~~~

And because -Wtype-limits is now globally disabled, such a workaround
now becomes useless. Remove the __is_nonneg()'s cast and its related
comment.

Signed-off-by: Vincent Mailhol <mailhol@...nel.org>
---
Changelog:

  v1 -> v2: new patch
---
 include/linux/minmax.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index a0158db54a04..3e2e3e539ba1 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -52,9 +52,6 @@
 /*
  * Check whether a signed value is always non-negative.
  *
- * A cast is needed to avoid any warnings from values that aren't signed
- * integer types (in which case the result doesn't matter).
- *
  * On 64-bit any integer or pointer type can safely be cast to 'long long'.
  * But on 32-bit we need to avoid warnings about casting pointers to integers
  * of different sizes without truncating 64-bit values so 'long' or 'long long'
@@ -65,7 +62,7 @@
  * but they are handled by the !is_signed_type() case).
  */
 #if __SIZEOF_POINTER__ == __SIZEOF_LONG_LONG__
-#define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
+#define __is_nonneg(ux) statically_true((ux) >= 0)
 #else
 #define __is_nonneg(ux) statically_true( \
 	(typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)))(ux) >= 0)

-- 
2.51.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ