Performing pointer arithmetic on a null pointer?

Hyeonggon Yoo 42.hyeyoo at gmail.com
Thu May 27 11:49:39 EDT 2021


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



More information about the Kernelnewbies mailing list