Re: [RFC] Clarification for “undefined behaviour”?
From: David Laight
Date: Wed Mar 05 2025 - 09:17:48 EST
On Wed, 5 Mar 2025 11:51:59 +0300
Dan Carpenter <dan.carpenter@xxxxxxxxxx> wrote:
> On Wed, Mar 05, 2025 at 09:40:43AM +0100, Markus Elfring wrote:
> > >>> The address of a data structure member was determined before
> > >>> a corresponding null pointer check in the implementation of
> > >>> the functions “dpu_hw_pp_enable_te” and “dpu_hw_pp_get_vsync_info”.
> > >>>
> > >>> Thus avoid the risk for undefined behaviour by removing extra
> > >>> initialisations for the variable “c” (also because it was already
> > >>> reassigned with the same value behind this pointer check).
> > > There is no undefined behavior here.
> >
> > Is there a need to improve the wording precision?
> >
> > There are words which denote a special meaning according to aspects of
> > the programming language “C”.
> > https://en.cppreference.com/w/c/language/behavior
> >
> > Dereferences of null pointers are treated in special ways.
>
> This not a dereference. It's just pointer math.
And the 'fun' starts because NULL isn't required to use the all-zero
bit pattern.
Regardless of the bit-pattern, things like (void *)(1 - 1) are valid
NULL pointers.
Of course, while C allows this, I doubt NULL has ever been other than 0.
(It was 0 on a system I used many years ago where the O/S invalid pointer
was ~0.)
I know Clang has started warning about arithmetic on NULL.
I wonder when it is going to start warning about memset(p, 0, sz)
for anything that contains a pointer - equally invalid.
David