Re: warning: dereferencing type-punned pointer

Peter Eisentraut <peter@eisentraut.org>

From: Peter Eisentraut <peter@eisentraut.org>
To: Tom Lane <tgl@sss.pgh.pa.us>, Tatsuo Ishii <ishii@postgresql.org>
Cc: pgsql-hackers@postgresql.org
Date: 2024-07-24T17:53:47Z
Lists: pgsql-hackers
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 *.  You wouldn't 
normally need to call IsA on that, but it comes with the 
SOFT_ERROR_OCCURRED macro.  Another difference is that most nodes are 
dynamically allocated but in this case the ErrorSaveContext object (not 
a pointer to it) is part of another struct, and so I think some 
different aliasing rules might apply, but I'm not sure.

I think here you could just bypass the SOFT_ERROR_OCCURRED macro:

-       if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
+       if (jsestate->escontext.error_occurred)



Commits

  1. Fix a strict aliasing violation

  2. Add input function for data type pg_ndistinct