Re: warning: dereferencing type-punned pointer

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Tatsuo Ishii <ishii(at)postgresql(dot)org>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: warning: dereferencing type-punned pointer
Date: 2024-07-24 18:26:55
Message-ID: fbfe2471-a001-49a6-a15b-3cb33b63e16f@eisentraut.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 24.07.24 20:09, Tom Lane wrote:
> Peter Eisentraut<peter(at)eisentraut(dot)org> writes:
>> On 24.07.24 16:05, Tom Lane wrote:
>>> I'm not very thrilled with these changes. It's not apparent why
>>> your compiler is warning about these usages of IsA and not any other
>>> ones,
>> I think one difference is that normally IsA is called on a Node * (since
>> you call IsA to decide what to cast it to), but in this case it's called
>> on a pointer that is already of type ErrorSaveContext *.
> Hmm. But there are boatloads of places where we call IsA on a
> pointer of type Expr *, or sometimes other things. Why aren't
> those triggering the same warning?

It must have to do with the fact that the escontext field in
JsonExprState has the object inline, not as a pointer. AIUI, with
dynamically allocated objects you have more liberties about what type to
interpret them as than with actually declared objects.

If you change the member to a pointer

- ErrorSaveContext escontext;
+ ErrorSaveContext *escontext;
} JsonExprState;

and make the required adjustments elsewhere in the code, the warning
goes away.

This arrangement would also appear to be more consistent with other
executor nodes (e.g., ExprState, ExprEvalStep), so it might be worth it
for consistency in any case.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2024-07-24 18:29:45 Re: warning: dereferencing type-punned pointer
Previous Message Peter Eisentraut 2024-07-24 18:10:45 Re: [18] Policy on IMMUTABLE functions and Unicode updates