0002-Fix-incorrect-Datum-conversion-in-timestamptz_trunc_.patch

text/plain

Filename: 0002-Fix-incorrect-Datum-conversion-in-timestamptz_trunc_.patch
Type: text/plain
Part: 1
Message: Re: date_trunc invalid units with infinite value

Patch

Format: format-patch
Series: patch 0002
Subject: Fix incorrect Datum conversion in timestamptz_trunc_internal()
File+
src/backend/utils/adt/timestamp.c 1 1
From 4f9e5dde59efc00c682f14a3047fd432c87f4209 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 6 Aug 2025 12:37:41 +0200
Subject: [PATCH 2/2] Fix incorrect Datum conversion in
 timestamptz_trunc_internal()

It used PG_RETURN_TIMESTAMPTZ(), but the return type is TimestampTz,
not Datum.  On 64-bit systems, there is no effect, since this just
ends up casting 64-bit integers back and forth.  But on 32-bit
systems, timestamptz is pass-by-reference, and so
PG_RETURN_TIMESTAMPTZ() allocates new memory and returns the address,
but the caller will try to interpret this as a timestamp value.  The
effect is that date_trunc(..., 'infinity'::timestamptz) will return
random values (instead of the correct return value 'infinity').

The fix is to use a straight "return" call.

Commit d85ce012f99 added this code but provided tests that did not
cover this correctly.  This was fixed in the previous patch.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/8246d7ff-f4b7-4363-913e-827dadfeb145%40eisentraut.org
---
 src/backend/utils/adt/timestamp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 25cff56c3d0..e640b48205b 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -4954,7 +4954,7 @@ timestamptz_trunc_internal(text *units, TimestampTz timestamp, pg_tz *tzp)
 				case DTK_SECOND:
 				case DTK_MILLISEC:
 				case DTK_MICROSEC:
-					PG_RETURN_TIMESTAMPTZ(timestamp);
+					return timestamp;
 					break;
 
 				default:
-- 
2.50.1