public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tatsuo Ishii <[email protected]>
To: [email protected]
Subject: warning: dereferencing type-punned pointer
Date: Wed, 24 Jul 2024 15:55:25 +0900 (JST)
Message-ID: <[email protected]> (raw)

Today I compiled PostgreSQL master branch with -fno-strict-aliasing
compile option removed (previous discussions on the $subject [1]). gcc
version is 9.4.0.

There are a few places where $subject warning printed.

In file included from ../../../src/include/nodes/pg_list.h:42,
                 from ../../../src/include/access/tupdesc.h:19,
                 from ../../../src/include/access/htup_details.h:19,
                 from ../../../src/include/access/heaptoast.h:16,
                 from execExprInterp.c:59:
execExprInterp.c: In function ‘ExecEvalJsonExprPath’:
../../../src/include/nodes/nodes.h:133:29: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  133 | #define nodeTag(nodeptr)  (((const Node*)(nodeptr))->type)
      |                            ~^~~~~~~~~~~~~~~~~~~~~~~
../../../src/include/nodes/nodes.h:158:31: note: in expansion of macro ‘nodeTag’
  158 | #define IsA(nodeptr,_type_)  (nodeTag(nodeptr) == T_##_type_)
      |                               ^~~~~~~
../../../src/include/nodes/miscnodes.h:53:26: note: in expansion of macro ‘IsA’
   53 |  ((escontext) != NULL && IsA(escontext, ErrorSaveContext) && \
      |                          ^~~
execExprInterp.c:4399:7: note: in expansion of macro ‘SOFT_ERROR_OCCURRED’
 4399 |   if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
      |       ^~~~~~~~~~~~~~~~~~~
execExprInterp.c: In function ‘ExecEvalJsonCoercionFinish’:
../../../src/include/nodes/nodes.h:133:29: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  133 | #define nodeTag(nodeptr)  (((const Node*)(nodeptr))->type)
      |                            ~^~~~~~~~~~~~~~~~~~~~~~~
../../../src/include/nodes/nodes.h:158:31: note: in expansion of macro ‘nodeTag’
  158 | #define IsA(nodeptr,_type_)  (nodeTag(nodeptr) == T_##_type_)
      |                               ^~~~~~~
../../../src/include/nodes/miscnodes.h:53:26: note: in expansion of macro ‘IsA’
   53 |  ((escontext) != NULL && IsA(escontext, ErrorSaveContext) && \
      |                          ^~~
execExprInterp.c:4556:6: note: in expansion of macro ‘SOFT_ERROR_OCCURRED’
 4556 |  if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
      |      ^~~~~~~~~~~~~~~~~~~
origin.c: In function ‘StartupReplicationOrigin’:
origin.c:773:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  773 |    file_crc = *(pg_crc32c *) &disk_state;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~

In my understanding from the discussion [1], it would be better to fix
our code to avoid the warning because it *might* point out that there
is something wrong with our code. However the consensus at the time
was, we will not remove -fno-strict-aliasing option for now. It will
take long time before it would happen...

So I think the warnings in ExecEvalJsonExprPath are better fixed
because these are the only places where IsA (nodeTag) macro are used
and the warning is printed. Patch attached.

I am not so sure about StartupReplicationOrigin. Should we fix it now?
For me the code looks sane as long as we keep -fno-strict-aliasing
option. Or maybe better to fix so that someday we could remove the
compiler option?

[1] https://www.postgresql.org/message-id/flat/366.1535731324%40sss.pgh.pa.us#bd93089182d13c79b74593ec70...

Best reagards,
--
Tatsuo Ishii
SRA OSS LLC
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp


Attachments:

  [text/x-patch] fix_ExecEvalJsonExprPath.patch (1.1K, ../[email protected]/2-fix_ExecEvalJsonExprPath.patch)
  download | inline diff:
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index d8735286c4..387311fdfb 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -4381,6 +4381,7 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
 	if (!*op->resnull && jsexpr->use_io_coercion)
 	{
 		FunctionCallInfo fcinfo;
+		Node	   *node;
 
 		Assert(jump_eval_coercion == -1);
 		fcinfo = jsestate->input_fcinfo;
@@ -4396,7 +4397,8 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
 
 		fcinfo->isnull = false;
 		*op->resvalue = FunctionCallInvoke(fcinfo);
-		if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
+		node = (Node *) &jsestate->escontext;
+		if (SOFT_ERROR_OCCURRED(node))
 			error = true;
 	}
 
@@ -4552,8 +4554,9 @@ void
 ExecEvalJsonCoercionFinish(ExprState *state, ExprEvalStep *op)
 {
 	JsonExprState *jsestate = op->d.jsonexpr.jsestate;
+	Node	   *node = (Node *) &jsestate->escontext;
 
-	if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
+	if (SOFT_ERROR_OCCURRED(node))
 	{
 		*op->resvalue = (Datum) 0;
 		*op->resnull = true;


view thread (10+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: warning: dereferencing type-punned pointer
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox