[PATCH] Handle out-of-range timestamps in timestamptz_to_str() - Mailing list pgsql-hackers

From Алена Васильева
Subject [PATCH] Handle out-of-range timestamps in timestamptz_to_str()
Date
Msg-id CABg3sZroJmuF+-c8Tswd4rNMVJEHVL94jttvoxBqA04PnJHLgw@mail.gmail.com
Whole thread Raw
List pgsql-hackers

Hello,

The function timestamptz_to_str() in src/bin/pg_waldump/compat.c does not
handle the case when localtime() returns NULL, which can happen for timestamps
that are out of the supported time_t range on some platforms.

This patch adds a simple check for a NULL return value and ensures that the
function returns a clear error string "(timestamp out of range)" instead of
causing undefined behavior or a crash.

Best regards,

Eugeny Goryachev


Patch attached for Postgres 16.6:

From b443fb1a5b3b0f0ffb419c2eb7a7375c9d5b990e Mon Sep 17 00:00:00 2001
From: Eugeny Goryachev <gorcom2012@gmail.com>
Date: Wed, 15 Oct 2025 12:41:10 +0400
Subject: [PATCH] Fix timestamptz_to_str() for out-of-range timestamps

---
src/bin/pg_waldump/compat.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/src/bin/pg_waldump/compat.c b/src/bin/pg_waldump/compat.c
index 2aca73f2e93..825311b1bd4 100644
--- a/src/bin/pg_waldump/compat.c
+++ b/src/bin/pg_waldump/compat.c
@@ -54,6 +54,12 @@ timestamptz_to_str(TimestampTz t)
time_t result = (time_t) timestamptz_to_time_t(t);
struct tm *ltime = localtime(&result);
+ if (!ltime)
+ {
+ strlcpy(buf, "(timestamp out of range)", sizeof(buf));
+ return buf;
+ }
+
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", ltime);
strftime(zone, sizeof(zone), "%Z", ltime);
--
2.43.0


https://www.postgresql.org/message-id/flat/EAB008E3-6B87-413F-BA39-22A6ED5DF0F6%40yesql.se#61ba59bee3872723d4cfe809638f039b
Attachment

pgsql-hackers by date:

Previous
From: Xuneng Zhou
Date:
Subject: Re: Improve read_local_xlog_page_guts by replacing polling with latch-based waiting
Next
From: Álvaro Herrera
Date:
Subject: Re: Implement waiting for wal lsn replay: reloaded