Обсуждение: [PATCH] Handle out-of-range timestamps in timestamptz_to_str()

Поиск
Список
Период
Сортировка

[PATCH] Handle out-of-range timestamps in timestamptz_to_str()

От
Алена Васильева
Дата:

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

Re: [PATCH] Handle out-of-range timestamps in timestamptz_to_str()

От
Chao Li
Дата:


On Oct 15, 2025, at 16:54, Алена Васильева <gorcom2012@gmail.com> wrote:

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.

I think we can just return a static const string, without copying the string into “buf" as the function returns a “const char *”.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/




Re: [PATCH] Handle out-of-range timestamps in timestamptz_to_str()

От
Daniel Gustafsson
Дата:
> On 15 Oct 2025, at 11:12, Chao Li <li.evan.chao@gmail.com> wrote:
>> On Oct 15, 2025, at 16:54, Алена Васильева <gorcom2012@gmail.com> wrote:

>> 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.

Ah yes, thanks for the reminder, it's been on my todo from
EAB008E3-6B87-413F-BA39-22A6ED5DF0F6@yesql.se for some time.

> I think we can just return a static const string, without copying the string into “buf" as the function returns a
“constchar *”. 

The function is documented as returning a value pointing to a static buffer, so
it seems better to consistently use that buffer.

--
Daniel Gustafsson