Re: warning: dereferencing type-punned pointer - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Re: warning: dereferencing type-punned pointer
Date
Msg-id 581db49e-2a12-4f89-8c3e-334ba5889ff7@eisentraut.org
Whole thread Raw
In response to Re: warning: dereferencing type-punned pointer  (Andres Freund <andres@anarazel.de>)
List pgsql-hackers
On 22.01.26 20:46, Andres Freund wrote:
> However, you qualified your answer with "to a different pointer-to-struct",
> but afaict the rules would be the same if the "initial member" of two
> different structs were a struct.
> 
> 
> There's also C23's §6.5 7):
>    An object shall have its stored value accessed only by an lvalue expression that has one of
>    the following types:
>    ...
>    — an aggregate or union type that includes one of the aforementioned types among its
>    members (including, recursively, a member of a subaggregate or contained union), or
> 
> which afaict means that if we *can* cast between different equivalent structs,
> as long as they have the same initial sequence?

I think what this means is that if you have

typedef struct Append
{
     Plan        plan;
     ...
}

and you have an object of type Plan, then you can access that object via 
a pointer of type Append.

Now that I see this again, this is the opposite the direction of what we 
would need (have object of type Append, access via pointer to Plan, or 
pointer to Node).  Also note that it doesn't require that member to be 
the first member.  So this consideration seems to be unrelated to what 
we are looking for.




pgsql-hackers by date:

Previous
From: Zsolt Parragi
Date:
Subject: Re: Remove redundant initialization of smgr pointer for relcache
Next
From: Anthonin Bonnefoy
Date:
Subject: Re: Fix rounding method used to compute huge pages