From: Sivirilova Marija <m.sivirilova@ftdata.ru>
To: Daniel Gustafsson <daniel@yesql.se>
Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Subject: RE: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
Date: Wed, 19 Aug 2026 09:13:58 +0000
Message-ID: <f159cf94e6694f11af783616234683dc@localhost.localdomain> (raw)
In-Reply-To: <5BDD13C9-D6C8-475D-9946-61A165C2C822@yesql.se>
References: <e3a21340a7844ada81c38aa5a164c58b@localhost.localdomain>
<5BDD13C9-D6C8-475D-9946-61A165C2C822@yesql.se>
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@yesql.se>
Sent: Wednesday, August 19, 2026 3:12 PM
To: Сивирилова Мария Константиновна <m.sivirilova@ftdata.ru>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
> On 19 Aug 2026, at 08:55, Sivirilova Marija <m.sivirilova@ftdata.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
Attachments:Fix-heap-buffer-overflow.patch (application/octet-stream, 3.2K) download
From eeb8e68b79e38d925ce7cac6ab2a356df9799bb9 Mon Sep 17 00:00:00 2001
From: Sivirilova Maria <m.sivirilova@ftdata.ru>
Date: Wed, 19 Aug 2026 16:05:32 +0700
Subject: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
When parsing format tokens '%D', '%r', '%R', or '%T', the function
allocated memory for the temporary buffer based on the length of the
remaining input string (pstr). However, it subsequently appended the
remaining format string (pfmt) to that buffer via strcat().
If the tail of the format string happened to be longer than the remaining
input string, a heap-buffer-overflow occurred.
Fix this by calculating the required buffer size using the length of
the format string (pfmt) instead of the input string (pstr).
Author: Maria Sivirilova <m.sivirilova@ftdata.ru>
Co-authored-by: Kanatbek Kanybekov <k.kanybekov@ftdata.ru>
---
src/interfaces/ecpg/pgtypeslib/dt_common.c | 8 ++++----
src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc | 9 +++++++++
2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/src/interfaces/ecpg/pgtypeslib/dt_common.c b/src/interfaces/ecpg/pgtypeslib/dt_common.cindex f0889dddaf9..6d58dee7890 100644--- a/src/interfaces/ecpg/pgtypeslib/dt_common.c+++ b/src/interfaces/ecpg/pgtypeslib/dt_common.c@@ -2658,7 +2658,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d,
* find the end of the substitution
*/
pfmt++;
- tmp = pgtypes_alloc(strlen("%m/%d/%y") + strlen(pstr) + 1);+ tmp = pgtypes_alloc(strlen("%m/%d/%y") + strlen(pfmt) + 1);
if (!tmp)
return 1;
strcpy(tmp, "%m/%d/%y");
@@ -2785,7 +2785,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d,
break;
case 'r':
pfmt++;
- tmp = pgtypes_alloc(strlen("%I:%M:%S %p") + strlen(pstr) + 1);+ tmp = pgtypes_alloc(strlen("%I:%M:%S %p") + strlen(pfmt) + 1);
if (!tmp)
return 1;
strcpy(tmp, "%I:%M:%S %p");
@@ -2795,7 +2795,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d,
return err;
case 'R':
pfmt++;
- tmp = pgtypes_alloc(strlen("%H:%M") + strlen(pstr) + 1);+ tmp = pgtypes_alloc(strlen("%H:%M") + strlen(pfmt) + 1);
if (!tmp)
return 1;
strcpy(tmp, "%H:%M");
@@ -2842,7 +2842,7 @@ PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp * d,
break;
case 'T':
pfmt++;
- tmp = pgtypes_alloc(strlen("%H:%M:%S") + strlen(pstr) + 1);+ tmp = pgtypes_alloc(strlen("%H:%M:%S") + strlen(pfmt) + 1);
if (!tmp)
return 1;
strcpy(tmp, "%H:%M:%S");
diff --git a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgcindex 62b934b07ee..101fccce6a8 100644--- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc+++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc@@ -66,6 +66,12 @@ main(void)
date *dc;
exec sql end declare section;
+ char large_fmt[10000] = {+ [0] = '%',+ [1] = 'D',+ [2 ... 9998] = 'A'+ };+
int i, j;
char *endptr;
@@ -147,5 +153,8 @@ main(void)
PGTYPESinterval_free(i1);
}
+ PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1);+ printf("Success parsing large string\n");+
return 0;
}
--
2.43.0
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: m.sivirilova@ftdata.ru, daniel@yesql.se
Subject: Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
In-Reply-To: <f159cf94e6694f11af783616234683dc@localhost.localdomain>
References: <e3a21340a7844ada81c38aa5a164c58b@localhost.localdomain> <5BDD13C9-D6C8-475D-9946-61A165C2C822@yesql.se> <f159cf94e6694f11af783616234683dc@localhost.localdomain>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox