* Re: Use of void pointer arithmetic?
2022-02-24 9:59 ` Use of void pointer arithmetic? Kalle Valo
@ 2022-02-24 10:31 ` Johannes Berg
2022-02-24 17:45 ` Linus Torvalds
1 sibling, 0 replies; 3+ messages in thread
From: Johannes Berg @ 2022-02-24 10:31 UTC (permalink / raw)
To: Kalle Valo, Dan Carpenter
Cc: Francesco Magliocca, Jeff Johnson, ath10k, rmanohar,
linux-wireless, linux-kernel, Linus Torvalds
On Thu, 2022-02-24 at 11:59 +0200, Kalle Valo wrote:
>
> A good question. I have always just thought we should avoid void pointer
> arithmetic due to the C standard, but now that you mention it void
> pointers can indeed simplify the code. So I'm not so sure anymore.
>
> Any opinions? Is there a kernel wide recommendation for this?
The kernel only enables it with W=3, which I guess nobody uses anyway
... Originally it came from commit 4a5838ad9d2d ("kbuild: Add extra gcc
checks") with a pointer to
http://marc.info/?l=kernel-janitors&m=129802065918147&w=2
(which is offline right now due to an expired certificate ...)
but setting back my clock it seems to point to
https://lore.kernel.org/all/20110218091716.GA4384@bicker/
but the thread kind of revolves around -Wconversion.
FreeBSD does enable -Wpointer-arith which is why we've been trying to
keep iwlwifi clean as a courtesy to them, but for really Linux-only code
I dunno if there's much point. Although of course that applies also to
FreeBSD ...
johannes
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: Use of void pointer arithmetic?
2022-02-24 9:59 ` Use of void pointer arithmetic? Kalle Valo
2022-02-24 10:31 ` Johannes Berg
@ 2022-02-24 17:45 ` Linus Torvalds
1 sibling, 0 replies; 3+ messages in thread
From: Linus Torvalds @ 2022-02-24 17:45 UTC (permalink / raw)
To: Kalle Valo
Cc: Dan Carpenter, Francesco Magliocca, Jeff Johnson, ath10k,
rmanohar, linux-wireless, Linux Kernel Mailing List
On Thu, Feb 24, 2022 at 1:59 AM Kalle Valo <kvalo@kernel.org> wrote:
>
> > What does -Wpointer-arith buy us?
>
> A good question. I have always just thought we should avoid void pointer
> arithmetic due to the C standard, but now that you mention it void
> pointers can indeed simplify the code. So I'm not so sure anymore.
>
> Any opinions? Is there a kernel wide recommendation for this?
We consciously use arithmetic on 'void *' in some places, although
it's not exactly _hugely_ common.
It's much more common to turn a pointer into an 'unsigned long' and
then doing basic operations on that.
The advantage of 'void *' is that it does avoid the need to cast the
pointer back.
But at the same time it will never replace the 'cast to an actual
integer type', because the 'void *' arithmetic only does simple
addition and subtraction, and many pointer operations need more (ie
alignment needs to do the bitops).
So I think it's mostly a personal preference. I *do* think that doing
arithmetic on 'void *' is generally superior to doing it the
old-fashioned C way on 'char *' - unless, of course, 'char *' is
simply the native type in question.
So if 'char *' casts (and casting back) is the alternative, then by
all means use 'void *' as a kind of generic and type-independent "byte
pointer". That is how it is meant to be used in the gcc extension.
And no, nobody should ever use -Wpointer-arith on the kernel in
general. Our use of it is not _hugely_ common, but it's does exist,
and it's not really frowned upon.
Linus
^ permalink raw reply [flat|nested] 3+ messages in thread