From ce87d2e53b33fb80de2706ffb3b0d9fc8016b834 Mon Sep 17 00:00:00 2001 From: rillig Date: Sat, 15 Oct 2022 21:19:15 +0000 Subject: [PATCH] tests/lint: add more examples for 'extra bits set to 0' Seen in sys/external/bsd/compiler_rt/dist/lib/builtins/fp_lib.h:88. --- tests/usr.bin/xlint/lint1/msg_309.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/usr.bin/xlint/lint1/msg_309.c b/tests/usr.bin/xlint/lint1/msg_309.c index 7fadd866f155..298a764e105c 100644 --- a/tests/usr.bin/xlint/lint1/msg_309.c +++ b/tests/usr.bin/xlint/lint1/msg_309.c @@ -1,4 +1,4 @@ -/* $NetBSD: msg_309.c,v 1.5 2022/06/17 06:59:16 rillig Exp $ */ +/* $NetBSD: msg_309.c,v 1.6 2022/10/15 21:19:15 rillig Exp $ */ # 3 "msg_309.c" // Test for message: extra bits set to 0 in conversion of '%s' to '%s', op '%s' [309] @@ -24,6 +24,33 @@ scale(unsigned long long x) { if ((x & 0xffff0000) != 0) return 16; + /* + * The integer constant is explicitly unsigned. Even in this case, + * the code may have originated on a platform where 'x' had 32 bits + * originally, and the intention may have been to clear the lower 16 + * bits. + */ + /* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */ + if ((x & 0xffff0000U) != 0) + return 16; + + /* + * Even if the expression is written as '& ~', which makes the + * intention of clearing the lower 16 bits clear, on a 32-bit + * platform the integer constant stays at 32 bits, and when porting + * the code to a 64-bit platform, the upper 32 bits are preserved. + */ + /* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */ + if ((x & ~0xffffU) != 0) + return 16; + + /* + * Casting the integer constant to the proper type removes all + * ambiguities about the programmer's intention. + */ + if ((x & (unsigned long long)~0xffffU) != 0) + return 16; + /* * In the remaining cases, the constant does not have its most * significant bit set, therefore there is no ambiguity.