All of lore.kernel.org
 help / color / mirror / Atom feed
* Performing pointer arithmetic on a null pointer?
@ 2021-05-27 15:14 Hyeonggon Yoo
  2021-05-27 15:27 ` Lukas Bulwahn
  2021-05-27 15:32 ` Greg KH
  0 siblings, 2 replies; 5+ messages in thread
From: Hyeonggon Yoo @ 2021-05-27 15:14 UTC (permalink / raw)
  To: kernelnewbies

Hello, I was compiling kernel with make CC=clang-10 W=1 -s

there are some places that compiler complains about
pointer arithmetic like below.it says it's undefined behavior.

is it just OK to use UBs like this (I hope it's not),
or am I missing something?

fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on
	a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
                return NULL + !*ppos;

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Performing pointer arithmetic on a null pointer?
  2021-05-27 15:14 Performing pointer arithmetic on a null pointer? Hyeonggon Yoo
@ 2021-05-27 15:27 ` Lukas Bulwahn
  2021-05-27 15:56   ` Hyeonggon Yoo
  2021-05-27 15:32 ` Greg KH
  1 sibling, 1 reply; 5+ messages in thread
From: Lukas Bulwahn @ 2021-05-27 15:27 UTC (permalink / raw)
  To: Hyeonggon Yoo; +Cc: kernelnewbies

On Thu, May 27, 2021 at 5:15 PM Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote:
>
> Hello, I was compiling kernel with make CC=clang-10 W=1 -s
>
> there are some places that compiler complains about
> pointer arithmetic like below.it says it's undefined behavior.
>
> is it just OK to use UBs like this (I hope it's not),
> or am I missing something?
>
> fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on
>         a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
>                 return NULL + !*ppos;
>
>

Hyeonggon,

there is a patch series from Arnd Bergmann, a well-known kernel
developer and janitor, hanging around here:

https://git.kernel.org/pub/scm/linux/kernel/git/arnd/playground.git/log/?h=warnings-5.10-2

It includes a patch to address this issue:

https://git.kernel.org/pub/scm/linux/kernel/git/arnd/playground.git/commit/?h=warnings-5.10-2&id=cda293a8ee8a2bc763c33828cbc9ff21af987d45

The patch title and commit message indicates that this was submitted
in multiple versions to the mailing list.

You can find more on its progress on the mailing list here:
https://lore.kernel.org/lkml/?q=seq_file%3A+fix+clang+warning+for+NULL+pointer+arithmetic

It seems that it has not landed in linux-next yet, though:

https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/?qt=grep&q=seq_file%3A+fix+clang+warning+for+NULL+pointer+arithmetic

The commit and discussion around it will give you much more technical
details on the issue and its resolution.

I hope this helps.


Lukas

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Performing pointer arithmetic on a null pointer?
  2021-05-27 15:14 Performing pointer arithmetic on a null pointer? Hyeonggon Yoo
  2021-05-27 15:27 ` Lukas Bulwahn
@ 2021-05-27 15:32 ` Greg KH
  2021-05-27 15:49   ` Hyeonggon Yoo
  1 sibling, 1 reply; 5+ messages in thread
From: Greg KH @ 2021-05-27 15:32 UTC (permalink / raw)
  To: Hyeonggon Yoo; +Cc: kernelnewbies

On Fri, May 28, 2021 at 12:14:43AM +0900, Hyeonggon Yoo wrote:
> Hello, I was compiling kernel with make CC=clang-10 W=1 -s
> 
> there are some places that compiler complains about
> pointer arithmetic like below.it says it's undefined behavior.
> 
> is it just OK to use UBs like this (I hope it's not),
> or am I missing something?
> 
> fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on
> 	a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
>                 return NULL + !*ppos;

Should work just fine, what problems have you found with that code?

Also happens in a few other places in the vfs layer.  Tricky code, but
it works as pointer math is valid C code :)

thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Performing pointer arithmetic on a null pointer?
  2021-05-27 15:32 ` Greg KH
@ 2021-05-27 15:49   ` Hyeonggon Yoo
  0 siblings, 0 replies; 5+ messages in thread
From: Hyeonggon Yoo @ 2021-05-27 15:49 UTC (permalink / raw)
  To: Greg KH; +Cc: kernelnewbies

On Thu, May 27, 2021 at 05:32:11PM +0200, Greg KH wrote:
> On Fri, May 28, 2021 at 12:14:43AM +0900, Hyeonggon Yoo wrote:
> > Hello, I was compiling kernel with make CC=clang-10 W=1 -s
> > 
> > there are some places that compiler complains about
> > pointer arithmetic like below.it says it's undefined behavior.
> > 
> > is it just OK to use UBs like this (I hope it's not),
> > or am I missing something?
> > 
> > fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on
> > 	a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
> >                 return NULL + !*ppos;
> 
> Should work just fine, what problems have you found with that code?
> 

Hello Greg.

Yeah, that should work fine. I just wanted to understand
warning that clang says!

> Also happens in a few other places in the vfs layer.  Tricky code, but
> it works as pointer math is valid C code :)

I'm searching about pointer arithmetic.

Documents say that pointer math on void pointer (as NULL is void
pointer) is illegal by C standards. (I can be missing something!)

if above is true,then is it OK because
it's valid on gcc and clang?

Thanks,
Hyeonggon

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Performing pointer arithmetic on a null pointer?
  2021-05-27 15:27 ` Lukas Bulwahn
@ 2021-05-27 15:56   ` Hyeonggon Yoo
  0 siblings, 0 replies; 5+ messages in thread
From: Hyeonggon Yoo @ 2021-05-27 15:56 UTC (permalink / raw)
  To: Lukas Bulwahn; +Cc: kernelnewbies

On Thu, May 27, 2021 at 05:27:06PM +0200, Lukas Bulwahn wrote:
> Hyeonggon,
> 
> there is a patch series from Arnd Bergmann, a well-known kernel
> developer and janitor, hanging around here:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/arnd/playground.git/log/?h=warnings-5.10-2
> 

Oh, Arnd Bergmann was already working on it!

> It includes a patch to address this issue:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/arnd/playground.git/commit/?h=warnings-5.10-2&id=cda293a8ee8a2bc763c33828cbc9ff21af987d45
> 
> The patch title and commit message indicates that this was submitted
> in multiple versions to the mailing list.
> 
> You can find more on its progress on the mailing list here:
> https://lore.kernel.org/lkml/?q=seq_file%3A+fix+clang+warning+for+NULL+pointer+arithmetic
> 

Thank you for giving some informations,
it's always fun to read mailing list that I can understand :)

> It seems that it has not landed in linux-next yet, though:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/?qt=grep&q=seq_file%3A+fix+clang+warning+for+NULL+pointer+arithmetic
> 

Yes I was doing it on linux-next.. :D

> The commit and discussion around it will give you much more technical
> details on the issue and its resolution.
> 
> I hope this helps.
> 
> Lukas

Thank you for giving some links. it will be much helpful.
I'm going to read it now!

Hyeonggon

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-05-27 15:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-27 15:14 Performing pointer arithmetic on a null pointer? Hyeonggon Yoo
2021-05-27 15:27 ` Lukas Bulwahn
2021-05-27 15:56   ` Hyeonggon Yoo
2021-05-27 15:32 ` Greg KH
2021-05-27 15:49   ` Hyeonggon Yoo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.