Thread: BUG #18745: /src/bin/pg_waldump/compat.c

BUG #18745: /src/bin/pg_waldump/compat.c

From
PG Bug reporting form
Date:
The following bug has been logged on the website:

Bug reference:      18745
Logged by:          Daniel Elishakov
Email address:      dan-eli@mail.ru
PostgreSQL version: 16.4
Operating system:   ubuntu 20.04
Description:

Pointer 'ltime', returned from function 'localtime' at compat.c:55, may be
NULL and is dereferenced at compat.c:57 by calling function 'strftime'.


Re: BUG #18745: /src/bin/pg_waldump/compat.c

From
Daniel Gustafsson
Date:
> On 9 Dec 2024, at 14:07, PG Bug reporting form <noreply@postgresql.org> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference:      18745
> Logged by:          Daniel Elishakov
> Email address:      dan-eli@mail.ru
> PostgreSQL version: 16.4
> Operating system:   ubuntu 20.04
> Description:
>
> Pointer 'ltime', returned from function 'localtime' at compat.c:55, may be
> NULL and is dereferenced at compat.c:57 by calling function 'strftime'.

That's a fair point, ISTM we should mimic what the backend timestamptz_to_str
does with something like the following.  We could inspect errno and come up
with other errors as well but keeping the copies reasonably in sync for rare
errors seems worthwhile.

diff --git a/src/bin/pg_waldump/compat.c b/src/bin/pg_waldump/compat.c
index 1541eac611..9e1a45af81 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);

--
Daniel Gustafsson