[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Dereferencing NULL. (Was: [PATCH v3] NULL.3const: Add documentation for

From: Ralph Corderoy
Subject: Dereferencing NULL. (Was: [PATCH v3] NULL.3const: Add documentation for NULL)
Date: Tue, 26 Jul 2022 13:25:37 +0100

Hi Alex and Ingo,

Alex wrote:
> > > +It is undefined behavior to dereference a null pointer
> > 
> > That is formally true, but hardly helpful in a manual page because
> > what happens when you dereference a NULL pointer is faily
> > predictable in practice: a segmentation fault.
> > 
> > Any other behaviour of the C language implementation would be such a
> > massive security risk that i don't think even the most avid compiler
> > optimizer would seriously consider it.
> >    While dereferencing a NULL pointer is formally undefined
> >    behaviour, it almost certainly causes a segmentation fault in
> >    practice.
> Oh, yeah, the act of dereferencing will very likely cause the process
> to segfault; it seems useful info to add.  Thanks!
> But also, the compiler can feel too smart about it, and silently
> remove other portions of code that it deems useless.

Its long been the case that straying into undefined behaviour allows a
compiler to optimise away what is stated above as ‘almost certain’,

    Dereferencing a NULL Pointer: contrary to popular belief,
    dereferencing a null pointer in C is undefined.
    It is not defined to trap...
    NULL pointer dereferences being undefined enables a broad range of
    In C-based languages, NULL being undefined enables a large number of
    simple scalar optimizations that are exposed as a result of macro
    expansion and inlining.

> But consider the broader ISO C scenario (or old POSIX.1-2001), where 
> NULL isn't defined to be (void*)0.  There it isn't so obvious that 0 
> would also represent a null pointer.

On a machine which uses a non-zero bit pattern as its NULL pointer,
the constant 0 in a pointer context will be that non-zero bit pattern.

I find the content of this man page for NULL seems confused.  It's
mixing reference material from standards with tutorial information for
beginners at C programming.  I think a man page should contain succinct
reference material and leave introductory or exploratory material to
accompanying papers.  Do the Linux man pages ship accompanying papers,
as the original ‘Editions’ of Unix did?  Perhaps that's an opening and
less constrained format?

Cheers, Ralph.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]