Re: [PATCH] RDMA/siw: Fix compiler warnings on 32-bit due to u64/pointer abuse

From: Joe Perches
Date: Tue Aug 27 2019 - 14:33:27 EST


On Tue, 2019-08-27 at 19:59 +0200, Geert Uytterhoeven wrote:
> On Tue, Aug 27, 2019 at 7:46 PM Al Viro <viro@xxxxxxxxxxxxxxxxxx> wrote:
> > On Tue, Aug 27, 2019 at 07:29:52PM +0200, Geert Uytterhoeven wrote:
> > > On Tue, Aug 27, 2019 at 4:17 PM David Laight <David.Laight@xxxxxxxxxx> wrote:
> > > > From: Geert Uytterhoeven
> > > > > Sent: 19 August 2019 18:15
> > > > ...
> > > > > > I think a cast to unsigned long is rather more common.
> > > > > >
> > > > > > uintptr_t is used ~1300 times in the kernel.
> > > > > > I believe a cast to unsigned long is much more common.

btw: apparently that's not true.

This grep may be incomplete but it seems there are fewer
kernel uses of a cast to unsigned long then pointer:

$ git grep -P '\(\s*\w+(\s+\w+){0,3}(\s*\*)+\s*\)\s*\(\s*unsigned\s+long\s*\)'|wc -l
423

Maybe add a cast_to_ptr macro like

#define cast_to_ptr(type, val) ((type)(uintptr_t)(val))

though that may not save any horizontal space

and/or a checkpatch test like:
---
scripts/checkpatch.pl | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 287fe73688f0..4ec88bc53f2f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6281,6 +6281,15 @@ sub process {
}
}

+# check for casts to pointer with intermediate casts to (unsigned long) not (uintptr_t)
+ if ($line =~ /\(\s*\w+(?:\s+\w+){0,3}(?:\s*\*){1,4}\s*\)\s*\(\s*unsigned\s+long\s*\)/) {
+ if (WARN("PREFER_UINTPTR_T",
+ "prefer intermediate cast to uintptr_t\n" . $herecurr) &&
+ $fix) {
+ $fixed[$fixlinenr] =~ s/(\(\s*\w+(?:\s+\w+){0,3}(?:\s*\*){1,4}\s*\))\s*\(\s*unsigned\s+long\s*\)/$1(uintptr_t)/;
+ }
+ }
+
# check for pointless casting of alloc functions
if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
WARN("UNNECESSARY_CASTS",