Re: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template
Date
Msg-id 3011659.1678834145@sss.pgh.pa.us
Whole thread Raw
In response to BUG #17839: Heap-buffer overflow on float8_to_char with invalid template  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template  (Thiago Nunes <thiagotnunes@gmail.com>)
List pgsql-bugs
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

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');

pgsql-bugs by date:

Previous
From: hubert depesz lubaczewski
Date:
Subject: pg_read_server_files doesn't let me use pg_ls_dir() or pg_read_file?
Next
From: Thiago Nunes
Date:
Subject: Re: BUG #17839: Heap-buffer overflow on float8_to_char with invalid template