pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: thiagotnunes@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template
Date: Tue, 14 Mar 2023 18:49:05 -0400
Message-ID: <3011659.1678834145@sss.pgh.pa.us> (raw)
In-Reply-To: <17839-aada50db24d7b0da@postgresql.org>
References: <17839-aada50db24d7b0da@postgresql.org>

PG Bug reporting form <noreply@postgresql.org> writes:
> Heap-buffer overflow on float8_to_char when format exceeds max double
> digits. I noticed this when running tests with memory sanitiser (msan).
> The following example triggers the failure (considering max double digits
> `DBL_DIG` is 15):
> float8_to_char(12345678901, "FM9999999999D999990")

Thanks for the report!  For reasons that aren't entirely clear to me,
valgrind didn't whine about this until I added a few more zeroes to the
format string, like

SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000');

Nonetheless, it did see the issue at that point.  I'm inclined to fix
it by making the code that adjusts Np->last_relevant more paranoid,
as attached.

			regards, tom lane

Attachments:

fix-wild-read-in-to_char.patch (text/x-diff, 2.1K) download diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index f3f4db5ef6..e6246dc44b 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -5695,13 +5695,20 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, /* * If any '0' specifiers are present, make sure we don't strip - * those digits. + * those digits. But don't advance last_relevant beyond the last + * character of the Np->number string, which is a hazard if the + * number got shortened due to precision limitations. */ if (Np->last_relevant && Np->Num->zero_end > Np->out_pre_spaces) { + int last_zero_pos; char *last_zero; - last_zero = Np->number + (Np->Num->zero_end - Np->out_pre_spaces); + /* note that Np->number cannot be zero-length here */ + last_zero_pos = strlen(Np->number) - 1; + last_zero_pos = Min(last_zero_pos, + Np->Num->zero_end - Np->out_pre_spaces); + last_zero = Np->number + last_zero_pos; if (Np->last_relevant < last_zero) Np->last_relevant = last_zero; } diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out index 2ac2c99b19..26930f4db4 100644 --- a/src/test/regress/expected/numeric.out +++ b/src/test/regress/expected/numeric.out @@ -1929,6 +1929,12 @@ SELECT to_char('100'::numeric, 'FM999'); 100 (1 row) +SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000'); + to_char +----------------- + ##########.#### +(1 row) + -- Check parsing of literal text in a format string SELECT to_char('100'::numeric, 'foo999'); to_char diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql index 6e9e6145d0..2dddb58625 100644 --- a/src/test/regress/sql/numeric.sql +++ b/src/test/regress/sql/numeric.sql @@ -979,6 +979,7 @@ FROM v; SELECT to_char('100'::numeric, 'FM999.9'); SELECT to_char('100'::numeric, 'FM999.'); SELECT to_char('100'::numeric, 'FM999'); +SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000'); -- Check parsing of literal text in a format string SELECT to_char('100'::numeric, 'foo999');

view thread (5+ messages)  latest in thread

Message-ID: <3011659.1678834145@sss.pgh.pa.us>
Permalink:  ../3011659.1678834145@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3011659.1678834145@sss.pgh.pa.us

 ·  · 

reply

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, thiagotnunes@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template
  In-Reply-To: <3011659.1678834145@sss.pgh.pa.us>

* 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