Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"

From: Andrey Rachitskiy <pl0h0yp1(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"
Date: 2026-08-11 14:31:26
Message-ID: CAB8bMiuv0T175FkbcFi_jGN1kp_0S_cYLyHXFiL7RvyuqqQrjg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi, Hackers!

PGTYPESdate_fmt_asc() replaces fixed-width tokens in place. "yyyy"
is four characters. Callers typically size outbuf as
strlen(fmtstring)+1, as dt_test does.

For year >= 10000, "%04u" produces five or more digits. memcpy()
over the "yyyy" span overruns that token and clobbers the trailing
NUL in a strlen(fmt)+1 buffer. ASan reports a heap-buffer-overflow
on the next strstr() in the token loop.
```
const char *fmt = "yyyy";
date d = PGTYPESdate_from_asc("10000-01-01", NULL);
char *out = malloc(strlen(fmt) + 1);

PGTYPESdate_fmt_asc(d, fmt, out);
```
The attached patch rejects a replacement longer than the token and
sets errno to PGTYPES_DATE_BAD_DATE. A dt_test case is included.

--
Regards,
Rachitskiy Andrey

Attachment Content-Type Size
0001-ecpg-date-fmt-asc-year-overflow.patch text/x-patch 6.3 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2026-08-11 14:38:13 Re: pg_control_checkpoint(): add "data_checksum_version" (Pg19)?
Previous Message Amit Kapila 2026-08-11 14:00:40 Re: Introduce XID age based replication slot invalidation