pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feed[PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
9+ messages / 3 participants
[nested] [flat]
* [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-08-19 06:55 Sivirilova Marija <m.sivirilova@ftdata.ru>
0 siblings, 1 reply; 9+ messages in thread
From: Sivirilova Marija @ 2026-08-19 06:55 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
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):
timestamp ts;
PGTYPEStimestamp_defmt_asc("1", "%D %H:%M:%S", &ts);
ERROR: AddressSanitizer: heap-buffer-overflow WRITE of size 10 at 0x... 0 bytes after 10-byte region
#0 strcat
#1 PGTYPEStimestamp_defmt_scan dt_common.c:2667
#2 PGTYPEStimestamp_defmt_asc timestamp.c:841 allocated by pgtypes_alloc common.c:12
The solution is to allocate memory based on the length of the format string (pfmt) instead of the input string (pstr), as it is the string being appended.
Best regards,
Maria Sivirilova
Kanatbek Kanybekov
Attachments:
Fix-heap-buffer-overflow.patch (application/octet-stream, 2.5K) download
From dd09b151ee6e70a5f7eafbd55a2e41f2c463357e Mon Sep 17 00:00:00 2001
From: Sivirilova Maria <m.sivirilova@ftdata.ru>
Date: Wed, 19 Aug 2026 11:59:55 +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 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/interfaces/ecpg/pgtypeslib/dt_common.c b/src/interfaces/ecpg/pgtypeslib/dt_common.c
index 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");
--
GitLab
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-08-19 08:11 Daniel Gustafsson <daniel@yesql.se>
parent: Sivirilova Marija <m.sivirilova@ftdata.ru>
0 siblings, 1 reply; 9+ messages in thread
From: Daniel Gustafsson @ 2026-08-19 08:11 UTC (permalink / raw)
To: Sivirilova Marija <m.sivirilova@ftdata.ru>; +Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
> 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
^ permalink raw reply [nested|flat] 9+ messages in thread
* RE: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-08-19 09:13 Sivirilova Marija <m.sivirilova@ftdata.ru>
parent: Daniel Gustafsson <daniel@yesql.se>
0 siblings, 1 reply; 9+ messages in thread
From: Sivirilova Marija @ 2026-08-19 09:13 UTC (permalink / raw)
To: Daniel Gustafsson <daniel@yesql.se>; +Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
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.c
index 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.pgc
index 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
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-08-19 09:43 Daniel Gustafsson <daniel@yesql.se>
parent: Sivirilova Marija <m.sivirilova@ftdata.ru>
0 siblings, 1 reply; 9+ messages in thread
From: Daniel Gustafsson @ 2026-08-19 09:43 UTC (permalink / raw)
To: Sivirilova Marija <m.sivirilova@ftdata.ru>; +Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
> On 19 Aug 2026, at 11:13, Sivirilova Marija <m.sivirilova@ftdata.ru> wrote:
>
> 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.
Thanks. For anyone reviewing (I at least won't have time for a few days):
there is also an earlier patch submitted for fixing this bug which should be
considered to make sure we pick the best fix.
https://postgr.es/m/CAB8bMiuv0T175FkbcFi_jGN1kp_0S_cYLyHXFiL7RvyuqqQrjg@mail.gmail.com
--
Daniel Gustafsson
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-08-19 21:56 Zsolt Parragi <zsolt.parragi@percona.com>
parent: Daniel Gustafsson <daniel@yesql.se>
0 siblings, 1 reply; 9+ messages in thread
From: Zsolt Parragi @ 2026-08-19 21:56 UTC (permalink / raw)
To: Daniel Gustafsson <daniel@yesql.se>; +Cc: pgsql-hackers@lists.postgresql.org, Sivirilova Marija <m.sivirilova@ftdata.ru>
Hello!
The fix itself looks good to me, I only seem some issues in the test.
--- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
+++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
If the test produces additional output, you should also update the
output file. (but also see my later comment about this)
+ char large_fmt[10000] = {
+ [0] = '%',
+ [1] = 'D',
+ [2 ... 9998] = 'A'
+ };
+
2...9998 is a GNU extension, it won't compile on MSVC.
+ PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1);
+ printf("Success parsing large string\n");
Won't this fail parsing? I am not saying that it wouldn't trigger an
asan/valgrind report when executed with that and the original code,
but that printed message is misleading. (it won't succeed ; we aren't
parsing a large string, it's a small string parsed with a large format
string). It also only tests 1 of the 4 fixed locations.
I probably wouldn't print anything for these tests, as the output
won't help deciding if the test failed or passed, it is always the
same. Maybe a comment explaining that these calls can trigger
asan/valgrind would be more useful?
> For anyone reviewing (I at least won't have time for a few days):
> there is also an earlier patch submitted for fixing this bug which should be
> considered to make sure we pick the best fix.
These seems to be similar but different issue to me.
^ permalink raw reply [nested|flat] 9+ messages in thread
* RE: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-11-03 06:07 Sivirilova Marija <m.sivirilova@ftdata.ru>
parent: Zsolt Parragi <zsolt.parragi@percona.com>
0 siblings, 1 reply; 9+ messages in thread
From: Sivirilova Marija @ 2026-11-03 06:07 UTC (permalink / raw)
To: Zsolt Parragi <zsolt.parragi@percona.com>; Daniel Gustafsson <daniel@yesql.se>; +Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Hello!
I apologize for the delay in sending the update, as your previous message unfortunately got lost in my inbox.
I have updated the patch to fix MSVC compatibility by replacing the GNU-specific range extension with memset(), removed the printf() and added an explanatory comment for ASAN/Valgrind, expanded the test coverage to check all four fixed format tokens (%D, %r, %R, %T).
The updated patch is attached.
Best regards,
Maria Sivirilova
От: Zsolt Parragi <zsolt.parragi@percona.com>
Дата: вторник, 3 ноября 2026 г. в 13:01
Кому: Daniel Gustafsson <daniel@yesql.se>
Копия: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>, Сивирилова Мария Константиновна <m.sivirilova@ftdata.ru>
Тема: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
Hello!
The fix itself looks good to me, I only seem some issues in the test.
--- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
+++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
If the test produces additional output, you should also update the
output file. (but also see my later comment about this)
+ char large_fmt[10000] = {
+ [0] = '%',
+ [1] = 'D',
+ [2 ... 9998] = 'A'
+ };
+
2...9998 is a GNU extension, it won't compile on MSVC.
+ PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1);
+ printf("Success parsing large string\n");
Won't this fail parsing? I am not saying that it wouldn't trigger an
asan/valgrind report when executed with that and the original code,
but that printed message is misleading. (it won't succeed ; we aren't
parsing a large string, it's a small string parsed with a large format
string). It also only tests 1 of the 4 fixed locations.
I probably wouldn't print anything for these tests, as the output
won't help deciding if the test failed or passed, it is always the
same. Maybe a comment explaining that these calls can trigger
asan/valgrind would be more useful?
> For anyone reviewing (I at least won't have time for a few days):
> there is also an earlier patch submitted for fixing this bug which should be
> considered to make sure we pick the best fix.
These seems to be similar but different issue to me.
Attachments:
Fix-heap-buffer-overflow.patch (application/octet-stream, 3.5K) download
From ca55dd350412ade8bb07f94d05740f2f7106a6a2 Mon Sep 17 00:00:00 2001
From: Sivirilova Maria <m.sivirilova@ftdata.ru>
Date: Tue, 3 Nov 2026 12:51:46 +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 ++++----
.../ecpg/test/pgtypeslib/dt_test2.pgc | 17 +++++++++++++++++
2 files changed, 21 insertions(+), 4 deletions(-)
diff --git a/src/interfaces/ecpg/pgtypeslib/dt_common.c b/src/interfaces/ecpg/pgtypeslib/dt_common.c
index c4119ab7932..ff345f7712f 100644
--- a/src/interfaces/ecpg/pgtypeslib/dt_common.c
+++ b/src/interfaces/ecpg/pgtypeslib/dt_common.c
@@ -2660,7 +2660,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");
@@ -2787,7 +2787,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");
@@ -2797,7 +2797,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");
@@ -2845,7 +2845,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.pgc
index 62b934b07ee..20feaf577d3 100644
--- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
+++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
@@ -66,6 +66,9 @@ main(void)
date *dc;
exec sql end declare section;
+ char large_fmt[10000];
+ const char *fmt_tokens = "DrRT";
+
int i, j;
char *endptr;
@@ -147,5 +150,19 @@ main(void)
PGTYPESinterval_free(i1);
}
+ /*
+ * Test parsing a small string with a very large format string.
+ * These calls are meant to trigger ASAN or Valgrind reports
+ * if a heap-buffer-overflow regression occurs.
+ */
+ memset(large_fmt, 'A', sizeof(large_fmt) - 1);
+ large_fmt[sizeof(large_fmt) - 1] = '\0';
+ large_fmt[0] = '%';
+ for (i = 0; fmt_tokens[i] != '\0'; i++)
+ {
+ large_fmt[1] = fmt_tokens[i];
+ PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1);
+ }
+
return 0;
}
--
2.53.0
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-11-04 21:59 Zsolt Parragi <zsolt.parragi@percona.com>
parent: Sivirilova Marija <m.sivirilova@ftdata.ru>
0 siblings, 1 reply; 9+ messages in thread
From: Zsolt Parragi @ 2026-11-04 21:59 UTC (permalink / raw)
To: Sivirilova Marija <m.sivirilova@ftdata.ru>; +Cc: pgsql-hackers@lists.postgresql.org
Hello!
Thanks for the update.
This part:
> --- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
> +++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
>
> If the test produces additional output, you should also update the output file. (but also see my later comment about this)
Is still not addressed in the latest patch, dt_test2 fails with the
patch applied.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-11-16 06:11 Sivirilova Marija <m.sivirilova@ftdata.ru>
parent: Zsolt Parragi <zsolt.parragi@percona.com>
0 siblings, 1 reply; 9+ messages in thread
From: Sivirilova Marija @ 2026-11-16 06:11 UTC (permalink / raw)
To: Zsolt Parragi <zsolt.parragi@percona.com>; +Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Hello!
I’ve updated expected/pgtypeslib-dt_test2.c to match the modified test. No changes to stdout or stderr are needed.
The revised patch is attached.
Best regards,
Maria Sivirilova
Attachments:
Fix-heap-buffer-overflow.patch (application/octet-stream, 4.5K) download
From 68f27bbbb946b5f5e2ffab8ccfb5d3aad2161142 Mon Sep 17 00:00:00 2001
From: Sivirilova Maria <m.sivirilova@ftdata.ru>
Date: Mon, 16 Nov 2026 13:09:36 +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/expected/pgtypeslib-dt_test2.c | 17 +++++++++++++++++
src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc | 17 +++++++++++++++++
3 files changed, 38 insertions(+), 4 deletions(-)
diff --git a/src/interfaces/ecpg/pgtypeslib/dt_common.c b/src/interfaces/ecpg/pgtypeslib/dt_common.c
index c4119ab7932..ff345f7712f 100644
--- a/src/interfaces/ecpg/pgtypeslib/dt_common.c
+++ b/src/interfaces/ecpg/pgtypeslib/dt_common.c
@@ -2660,7 +2660,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");
@@ -2787,7 +2787,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");
@@ -2797,7 +2797,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");
@@ -2845,7 +2845,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/expected/pgtypeslib-dt_test2.c b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c
index b3e1e7519df..dae7fe3e157 100644
--- a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c
+++ b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c
@@ -101,6 +101,9 @@ main(void)
#line 67 "dt_test2.pgc"
+ char large_fmt[10000];
+ const char *fmt_tokens = "DrRT";
+
int i, j;
char *endptr;
@@ -182,5 +185,19 @@ main(void)
PGTYPESinterval_free(i1);
}
+ /*
+ * Test parsing a small string with a very large format string.
+ * These calls are meant to trigger ASAN or Valgrind reports
+ * if a heap-buffer-overflow regression occurs.
+ */
+ memset(large_fmt, 'A', sizeof(large_fmt) - 1);
+ large_fmt[sizeof(large_fmt) - 1] = '\0';
+ large_fmt[0] = '%';
+ for (i = 0; fmt_tokens[i] != '\0'; i++)
+ {
+ large_fmt[1] = fmt_tokens[i];
+ PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1);
+ }
+
return 0;
}
diff --git a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
index 62b934b07ee..20feaf577d3 100644
--- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
+++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc
@@ -66,6 +66,9 @@ main(void)
date *dc;
exec sql end declare section;
+ char large_fmt[10000];
+ const char *fmt_tokens = "DrRT";
+
int i, j;
char *endptr;
@@ -147,5 +150,19 @@ main(void)
PGTYPESinterval_free(i1);
}
+ /*
+ * Test parsing a small string with a very large format string.
+ * These calls are meant to trigger ASAN or Valgrind reports
+ * if a heap-buffer-overflow regression occurs.
+ */
+ memset(large_fmt, 'A', sizeof(large_fmt) - 1);
+ large_fmt[sizeof(large_fmt) - 1] = '\0';
+ large_fmt[0] = '%';
+ for (i = 0; fmt_tokens[i] != '\0'; i++)
+ {
+ large_fmt[1] = fmt_tokens[i];
+ PGTYPEStimestamp_defmt_asc("1 minute", large_fmt, &ts1);
+ }
+
return 0;
}
--
2.53.0
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
@ 2026-11-17 22:08 Zsolt Parragi <zsolt.parragi@percona.com>
parent: Sivirilova Marija <m.sivirilova@ftdata.ru>
0 siblings, 0 replies; 9+ messages in thread
From: Zsolt Parragi @ 2026-11-17 22:08 UTC (permalink / raw)
To: Sivirilova Marija <m.sivirilova@ftdata.ru>; +Cc: pgsql-hackers@lists.postgresql.org
Thanks, latest version looks good to me.
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2026-11-17 22:08 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-19 06:55 [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan() Sivirilova Marija <m.sivirilova@ftdata.ru>
2026-08-19 08:11 ` Daniel Gustafsson <daniel@yesql.se>
2026-08-19 09:13 ` Sivirilova Marija <m.sivirilova@ftdata.ru>
2026-08-19 09:43 ` Daniel Gustafsson <daniel@yesql.se>
2026-08-19 21:56 ` Zsolt Parragi <zsolt.parragi@percona.com>
2026-11-03 06:07 ` Sivirilova Marija <m.sivirilova@ftdata.ru>
2026-11-04 21:59 ` Zsolt Parragi <zsolt.parragi@percona.com>
2026-11-16 06:11 ` Sivirilova Marija <m.sivirilova@ftdata.ru>
2026-11-17 22:08 ` Zsolt Parragi <zsolt.parragi@percona.com>
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