Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix corner case bug in numeric to_char() some more.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 14 Mar 2023 23:17:31 +0000 (19:17 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 14 Mar 2023 23:17:31 +0000 (19:17 -0400)
The band-aid applied in commit f0bedf3e4 turns out to still need
some work: it made sure we didn't set Np->last_relevant too small
(to the left of the decimal point), but it didn't prevent setting
it too large (off the end of the partially-converted string).
This could result in fetching data beyond the end of the allocated
space, which with very bad luck could cause a SIGSEGV, though
I don't see any hazard of interesting memory disclosure.

Per bug #17839 from Thiago Nunes.  The bug's pretty ancient,
so back-patch to all supported versions.

Discussion: https://postgr.es/m/17839-aada50db24d7b0da@postgresql.org

src/backend/utils/adt/formatting.c
src/test/regress/expected/numeric.out
src/test/regress/sql/numeric.sql

index a1145e2721db9f52b5e084a2f568d11e5ec86a4a..5bd4d1967b9cc3374ca15a29475d6809d5dc2fb0 100644 (file)
@@ -5684,13 +5684,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;
            }
index bea33181bee8c888306b6751838af5f90fae6690..2f2818e001c1523a6965e7310df7ee50581ff871 100644 (file)
@@ -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 
index 9233c666d4bb874ec3a9d9e85982697906866ff2..56294da5ae9a5538f7f413db424255f1ea06d942 100644 (file)
@@ -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');