RE: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()

From: Sivirilova Marija <m(dot)sivirilova(at)ftdata(dot)ru>
To: Daniel Gustafsson <daniel(at)yesql(dot)se>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: RE: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
Date: 2026-08-19 09:13:58
Message-ID: f159cf94e6694f11af783616234683dc@localhost.localdomain
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I'm sending you an updated patch. A test has been added to src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc. It works without compiling AddressSanitizer.

Test result before patch:
...
interval_copy[5]: @ 1 year 59 mins
Error parsing interval 6
free(): invalid next size (fast)
Aborted (core dumped)

After patch:
...
interval_copy[5]: @ 1 year 59 mins
Error parsing interval 6
Success parsing large string

--
Maria Sivirilova

-----Original Message-----
From: Daniel Gustafsson <daniel(at)yesql(dot)se>
Sent: Wednesday, August 19, 2026 3:12 PM
To: Сивирилова Мария Константиновна <m(dot)sivirilova(at)ftdata(dot)ru>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()

> On 19 Aug 2026, at 08:55, Sivirilova Marija <m(dot)sivirilova(at)ftdata(dot)ru> wrote:
>
> Hello, hackers!
> We used the Clang Static Analyzer and found a heap-buffer-overflow in the PGTYPEStimestamp_defmt_scan() function inside src/interfaces/ecpg/pgtypeslib/dt_common.c. When processing template tokens like '%D', '%r', '%R', or '%T', the code allocates a temporary buffer based on the length of the remaining input string (pstr). However, it later concatenates the remaining format string (pfmt) into this buffer using strcat(). If an application attempts to parse a short or truncated time string using a longer, complex format pattern, the format string suffix will exceed the allocated memory, causing a heap-buffer-overflow.
> The issue can be reproduced with a minimal test case (compiling this with AddressSanitizer):

Can you please add testcases to the patch, to make sure we cover these edgecases in the regression tests.

--
Daniel Gustafsson

Attachment Content-Type Size
Fix-heap-buffer-overflow.patch application/octet-stream 3.2 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2026-08-19 09:25:52 Re: pg_control_checkpoint(): add "data_checksum_version" (Pg19)?
Previous Message Jelte Fennema-Nio 2026-08-19 08:59:16 Python/pytest test framework take two