pgsql: Fix incorrect Datum conversion in timestamptz_trunc_internal()

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix incorrect Datum conversion in timestamptz_trunc_internal()
Date: 2025-08-07 02:02:29
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix incorrect Datum conversion in timestamptz_trunc_internal()

The code used a PG_RETURN_TIMESTAMPTZ() where the return type is
TimestampTz and not a Datum.

On 64-bit systems, there is no effect since this just ends up casting
64-bit integers back and forth. On 32-bit systems, timestamptz is
pass-by-reference. PG_RETURN_TIMESTAMPTZ() allocates new memory and
returns the address, meaning that the caller could interpret this as a
timestamp value.

The effect is using "date_trunc(..., 'infinity'::timestamptz) will
return random values (instead of the correct return value 'infinity').

Bug introduced in commit d85ce012f99f.

Author: Peter Eisentraut <peter(at)eisentraut(dot)org>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/[email protected]
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/2242b26ce472db9ac69dc71008c566ea9cd3a5fd

Modified Files
--------------
src/backend/utils/adt/timestamp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2025-08-07 02:50:01 pgsql: Improve tests of date_trunc() with infinity and unsupported unit
Previous Message Nathan Bossart 2025-08-06 18:37:28 pgsql: Expand usage of macros for protocol characters.