diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2023-03-14 19:17:31 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2023-03-14 19:17:31 -0400 |
commit | 386a26023afb624726fb756ffdbef5b5832caa96 (patch) | |
tree | 54806c76ace49b1b2d3530d7a68a569e21e96f65 /src | |
parent | 52e9a781656edefb10e8371d46a1a6d62c1bbb11 (diff) | |
download | postgresql-386a26023afb624726fb756ffdbef5b5832caa96.tar.gz postgresql-386a26023afb624726fb756ffdbef5b5832caa96.zip |
Fix corner case bug in numeric to_char() some more.
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
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/utils/adt/formatting.c | 11 | ||||
-rw-r--r-- | src/test/regress/expected/numeric.out | 6 | ||||
-rw-r--r-- | src/test/regress/sql/numeric.sql | 1 |
3 files changed, 16 insertions, 2 deletions
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index f1a753fc91c..e4ffecb9d85 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -5685,13 +5685,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 4ca9fd64088..a9cfa533e06 100644 --- a/src/test/regress/expected/numeric.out +++ b/src/test/regress/expected/numeric.out @@ -1266,6 +1266,12 @@ SELECT '' AS to_char_26, 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 '' AS to_char_27, to_char('100'::numeric, 'foo999'); to_char_27 | to_char diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql index 34f96d1aab3..7d542d1ab84 100644 --- a/src/test/regress/sql/numeric.sql +++ b/src/test/regress/sql/numeric.sql @@ -799,6 +799,7 @@ SELECT '' AS to_char_23, to_char(val, '9.999EEEE') FROM num_data; SELECT '' AS to_char_24, to_char('100'::numeric, 'FM999.9'); SELECT '' AS to_char_25, to_char('100'::numeric, 'FM999.'); SELECT '' AS to_char_26, to_char('100'::numeric, 'FM999'); +SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000'); -- Check parsing of literal text in a format string SELECT '' AS to_char_27, to_char('100'::numeric, 'foo999'); |