From 61bb2214892402083ea581d558320b636e7968ac Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 20 Oct 2026 12:56:14 -0400 Subject: [PATCH] Fix to_char() for number formats ending in a decimal point. When a number format has a decimal point with no digit positions after it (e.g. '9.' or 'FM999.PL'), NUM_numpart_to_char() copied the terminating '\0' of the number string into the output as if it were a digit, resulting in apparent truncation of the output; for example, a sign or literal text following the decimal point would disappear. Fix it by not copying the '\0'. This is clearly a bug, but given the lack of field complaints, people who have run into it may think it's expected behavior, and may even have written applications that depend on it. Therefore it seems unwise to change it in stable branches. I (tgl) think it's not too late to fix it in v19 though. Bug: #19771 Author: Gowtham Reddy Reviewed-by: Tom Lane Discussion: https://postgr.es/m/19771-4f173b891525020c@postgresql.org Backpatch-through: 19 --- src/backend/utils/adt/formatting.c | 4 ++-- src/test/regress/expected/numeric.out | 6 ++++++ src/test/regress/sql/numeric.sql | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index ef5ec3227a1..4a5112bd49d 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -5804,14 +5804,14 @@ NUM_numpart_to_char(NUMProc *Np, int id) ++Np->inout_p; } } - else + else if (*Np->number_p) { *Np->inout_p = *Np->number_p; /* Write DIGIT */ ++Np->inout_p; Np->num_in = true; } } - /* do no exceed string length */ + /* do not advance past end of string */ if (*Np->number_p) ++Np->number_p; } diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out index c58e232a263..79c2eae8e61 100644 --- a/src/test/regress/expected/numeric.out +++ b/src/test/regress/expected/numeric.out @@ -2116,6 +2116,12 @@ SELECT to_char('100'::numeric, 'FM999.'); 100 (1 row) +SELECT to_char('100'::numeric, 'FM999.SG "items"'); + to_char +------------ + 100+ items +(1 row) + SELECT to_char('100'::numeric, 'FM999'); to_char --------- diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql index 640c6d92f4c..140fe30e8b7 100644 --- a/src/test/regress/sql/numeric.sql +++ b/src/test/regress/sql/numeric.sql @@ -1033,6 +1033,7 @@ FROM v; SELECT to_char('100'::numeric, 'FM999.9'); SELECT to_char('100'::numeric, 'FM999.'); +SELECT to_char('100'::numeric, 'FM999.SG "items"'); SELECT to_char('100'::numeric, 'FM999'); SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000');