public inbox for [email protected]  
help / color / mirror / Atom feed
From: Jianghua Yang <[email protected]>
To: [email protected]
Subject: [PATCH] Fix wrong argument to SOFT_ERROR_OCCURRED in timestamptz_date
Date: Tue, 24 Mar 2026 08:44:29 -0700
Message-ID: <CAAZLFmSGti716gWeY=DCZ9TTVOixnHZ4_4V4tDzoeE86D64vOA@mail.gmail.com> (raw)

 Hi hackers,

  I found a small bug in commit e2f289e5b9b ("Make many cast functions
error safe").

  In timestamptz_date(), the SOFT_ERROR_OCCURRED() check mistakenly
  uses fcinfo->args instead of fcinfo->context:

  result = timestamptz2date_safe(timestamp, fcinfo->context);
  if (SOFT_ERROR_OCCURRED(fcinfo->args))   /* should be fcinfo->context */
      PG_RETURN_NULL();

  fcinfo->args is a NullableDatum[] array, not a Node *. The
  SOFT_ERROR_OCCURRED macro casts its argument to Node * and reads
  the NodeTag field. When given fcinfo->args, it interprets the first
  argument's Datum value (a TimestampTz) as a NodeTag, which will
  almost never match T_ErrorSaveContext. This causes the soft error
  check to always evaluate to false.

  As a result, when the timestamptz-to-date conversion encounters an
  overflow in error-safe mode, the function returns a wrong date value
  instead of the expected NULL.

  All three sibling functions modified in the same commit (date_timestamp,
  timestamp_date, date_timestamptz) correctly use fcinfo->context.
  This appears to be a copy-paste oversight.

  The fix is a one-line change: fcinfo->args → fcinfo->context.


Attachments:

  [application/octet-stream] v1-0001-Fix-wrong-argument-to-SOFT_ERROR_OCCURRED-in-time.patch (1.6K, 3-v1-0001-Fix-wrong-argument-to-SOFT_ERROR_OCCURRED-in-time.patch)
  download | inline diff:
From f553acbaafa8c06eb2a37cadff1c838aad3cb70f Mon Sep 17 00:00:00 2001
From: Jianghua Yang <[email protected]>
Date: Tue, 24 Mar 2026 08:35:32 -0700
Subject: [PATCH v1] Fix wrong argument to SOFT_ERROR_OCCURRED in
 timestamptz_date

In commit e2f289e5b9b, which made many cast functions error safe,
timestamptz_date() mistakenly passes fcinfo->args to the
SOFT_ERROR_OCCURRED() macro instead of fcinfo->context.

fcinfo->args is a NullableDatum[] array, not a Node pointer. The macro
casts its argument to Node* and checks the NodeTag field. When given
fcinfo->args, it reads the first argument's Datum value as a NodeTag,
which will almost certainly not match T_ErrorSaveContext, causing the
soft error check to always evaluate to false.

As a result, when the timestamptz-to-date conversion encounters an
overflow in error-safe mode, the function will return a garbage date
value instead of the expected NULL.

All three sibling functions modified in the same commit
(date_timestamp, timestamp_date, date_timestamptz) correctly use
fcinfo->context.

Author: Jianghua Yang <[email protected]>
---
 src/backend/utils/adt/date.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c
index 71ea048d251..c3327440380 100644
--- a/src/backend/utils/adt/date.c
+++ b/src/backend/utils/adt/date.c
@@ -1402,7 +1402,7 @@ timestamptz_date(PG_FUNCTION_ARGS)
 	DateADT		result;
 
 	result = timestamptz2date_safe(timestamp, fcinfo->context);
-	if (SOFT_ERROR_OCCURRED(fcinfo->args))
+	if (SOFT_ERROR_OCCURRED(fcinfo->context))
 		PG_RETURN_NULL();
 
 	PG_RETURN_DATEADT(result);
-- 
2.50.1 (Apple Git-155)



view thread (5+ 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], [email protected]
  Subject: Re: [PATCH] Fix wrong argument to SOFT_ERROR_OCCURRED in timestamptz_date
  In-Reply-To: <CAAZLFmSGti716gWeY=DCZ9TTVOixnHZ4_4V4tDzoeE86D64vOA@mail.gmail.com>

* 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