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

Started by Andrey Rachitskiy3 months ago7 messageshackers
Jump to latest
#1Andrey Rachitskiy
pl0h0yp1@gmail.com

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

Attachments:

t253376_1
0001-ecpg-date-fmt-asc-year-overflow.patchtext/x-patch; charset=US-ASCII; name=0001-ecpg-date-fmt-asc-year-overflow.patchDownload+66-29
#2Andrey Rachitskiy
pl0h0yp1@gmail.com
In reply to: Andrey Rachitskiy (#1)
Re: Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"

вт, 11 авг. 2026 г. в 19:31, Andrey Rachitskiy <pl0h0yp1@gmail.com>:

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.

Hi, All!

Maybe someone will have some free time to do a review. Thanks a lot in
advance.

--
Regards,
Rachitskiy Andrey

#3Zsolt Parragi
zsolt.parragi@percona.com
In reply to: Andrey Rachitskiy (#2)
Re: Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"

Hello!

-[NO_PID]: ECPGtrans on line 393: action "rollback"; connection "ecpg1_regression"
+[NO_PID]: ECPGtrans on line 412: action "rollback"; connection "ecpg1_regression"

The modified test is failing in my run, it seems this should be 409?

#4Andrey Rachitskiy
pl0h0yp1@gmail.com
In reply to: Zsolt Parragi (#3)
Re: Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"

чт, 20 авг. 2026 г. в 03:17, Zsolt Parragi <zsolt.parragi@percona.com>:

Hello!

-[NO_PID]: ECPGtrans on line 393: action "rollback"; connection
"ecpg1_regression"
+[NO_PID]: ECPGtrans on line 412: action "rollback"; connection
"ecpg1_regression"

The modified test is failing in my run, it seems this should be 409?

Dear Zsolt,

Thanks for the review.

After shortening the test comment I updated the expected .c #line
markers but forgot the matching stderr line number. Fixed in the
attached v2.

--
Regards,
Rachitskiy Andrey

Attachments:

t253376_4
v2-0001-ecpg-date-fmt-asc-year-overflow.patchtext/x-patch; charset=US-ASCII; name=v2-0001-ecpg-date-fmt-asc-year-overflow.patchDownload+66-29
#5Andrey Rachitskiy
pl0h0yp1@gmail.com
In reply to: Andrey Rachitskiy (#4)
Re: Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"

чт, 20 авг. 2026 г. в 12:06, Andrey Rachitskiy <pl0h0yp1@gmail.com>:

чт, 20 авг. 2026 г. в 03:17, Zsolt Parragi <zsolt.parragi@percona.com>:

Hello!

-[NO_PID]: ECPGtrans on line 393: action "rollback"; connection
"ecpg1_regression"
+[NO_PID]: ECPGtrans on line 412: action "rollback"; connection
"ecpg1_regression"

The modified test is failing in my run, it seems this should be 409?

Dear Zsolt,

Thanks for the review.

After shortening the test comment I updated the expected .c #line
markers but forgot the matching stderr line number. Fixed in the
attached v2.

Hello everybody!

Reminder on this patch.
Have a good end of the week and enjoy the weekend.

--
Regards,
Rachitskiy Andrey

#6Andrey Rachitskiy
pl0h0yp1@gmail.com
In reply to: Andrey Rachitskiy (#5)
Re: Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"

пт, 4 сент. 2026 г. в 10:01, Andrey Rachitskiy <pl0h0yp1@gmail.com>:

чт, 20 авг. 2026 г. в 12:06, Andrey Rachitskiy <pl0h0yp1@gmail.com>:

чт, 20 авг. 2026 г. в 03:17, Zsolt Parragi <zsolt.parragi@percona.com>:

Hello!

-[NO_PID]: ECPGtrans on line 393: action "rollback"; connection
"ecpg1_regression"
+[NO_PID]: ECPGtrans on line 412: action "rollback"; connection
"ecpg1_regression"

The modified test is failing in my run, it seems this should be 409?

Dear Zsolt,

Thanks for the review.

After shortening the test comment I updated the expected .c #line
markers but forgot the matching stderr line number. Fixed in the
attached v2.

Hello everybody!

Reminder on this patch.
Have a good end of the week and enjoy the weekend.

Hello everybody!

Reminder on this patch :)

--
Regards,
Rachitskiy Andrey

#7David Rowley
dgrowleyml@gmail.com
In reply to: Andrey Rachitskiy (#6)
Re: Fix PGTYPESdate_fmt_asc overflow when a year does not fit "yyyy"

On Thu, 8 Oct 2026 at 21:03, Andrey Rachitskiy <pl0h0yp1@gmail.com> wrote:

Reminder on this patch :)

Please do not send reminders. It is not a scalable thing to do. The
commitfest application is meant to serve the purpose of not forgetting
patches. Please use it.

David