Some subsurface notes from a week of diving

Linus Torvalds torvalds at linux-foundation.org
Mon Mar 17 12:07:59 PDT 2014


On Mon, Mar 17, 2014 at 11:12 AM, Linus Torvalds
<torvalds at linux-foundation.org> wrote:
>
> Where in the standard does it say that you can't do arithmetic before
> testing a NULL pointer, and who can I shoot at for messing things up *again*
> if it actually does say that?

I don't have access to the real standard, but I do have access to the
draft standard that is public, and can find no sign of language saying
that taking the address of a member generates undefined behavior.

I can (obviously) find language that says that accessing a pointer
outside of its range is undefined, and sadly the whole "&x->y" thing
isn't as clearly taken to be just an address as the "&*x" and "&x[i]"
cases in 6.5.3.3. But it does talk about generating address constants
using & and ->, and makes it clear that in those cases the value of
the object is not to be accessed.

So I see no reason to buy into your interpretation that doing

  a = &b->c;

without actually ever accessing the object pointed to be a or b
implies that the compiler can assume that 'b' is non-NULL.

                    Linus


More information about the subsurface mailing list