pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d)
5+ messages / 3 participants
[nested] [flat]
Commits from this thread:
2026-10-20 61bb221489 776df8a6d7 Fix to_char() for number formats ending in a decimal point.
* BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d)
@ 2026-10-18 20:40 PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 5+ messages in thread
From: PG Bug reporting form @ 2026-10-18 20:40 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: gowthamdoma18@gmail.com
The following bug has been logged on the website:
Bug reference: 19771
Logged by: Gowtham Reddy
Email address: gowthamdoma18@gmail.com
PostgreSQL version: 19rc1
Operating system: Linux x86_64
Description:
Hello,
On master (tested at 45fc51fcbf45), to_char() for numbers returns a text
value containing a NUL byte when the format has a decimal point with no
digit positions after it:
SELECT encode(textsend(to_char(7, '9.')), 'hex');
-- master: 203700 REL_19: 2037
SELECT encode(textsend(to_char(7, '9.PL')), 'hex');
-- master: 2037002b REL_19: 2037
SELECT length(to_char(7, '9.')), octet_length(to_char(7, '9.'));
-- master: 2, 3
SELECT to_char(7, '9.') = ' 7';
-- master: false REL_19: true
SELECT encode(textsend(to_char('100'::numeric, 'FM999.')), 'hex');
-- master: 31303000
The NUL byte is also written to tables: CREATE TABLE t AS SELECT
to_char(7, '9.PL') stores 2037002b. The existing regression query in
numeric.sql (SELECT to_char('100'::numeric, 'FM999.')) returns '100\0' on
master but still passes, because psql stops printing at the NUL.
Bisected to:
ec352b37d Replace formatting.c's fixed-size output buffers with
StringInfos.
Its parent 28c5d0724 returns 2037 for the second query.
The underlying problem is older. In NUM_numpart_to_char(), the "Write
Digits" branch copies *Np->input_p even when the number string is already
exhausted, so it copies the terminating '\0'. With the old fixed-size
buffer that NUL ended the C string, so in released branches everything
after the trailing decimal point is silently dropped, including the sign:
SELECT to_char(-7, '9.MI'); -- REL_19: '7' (sign lost)
SELECT to_char(-7, '9.PR'); -- REL_19: '<7' (closing bracket lost)
SELECT to_char(7, '9.PL'); -- REL_19: ' 7' (PL lost)
SELECT to_char(7, '9."x"'); -- REL_19: ' 7' (literal lost)
With the StringInfo output buffer the NUL is kept, and the suffix appears
after it.
Proposed patch (attached, v1-0001): do not emit the string terminator as a
digit. It also adds a regression test to numeric.sql, which fails without
the fix. The core change is:
inline patch 1: src/backend/utils/adt/formatting.c (+1/-1 lines) download inline-1.patch
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -5659,7 +5659,7 @@ NUM_numpart_to_char(NUMProc *Np, int id)
NUM_EMITC('0');
}
}
- else
+ else if (*Np->input_p != '\0')
{
NUM_EMITC(*Np->input_p); /*
Write DIGIT */
Np->num_in = true;
With this, master returns ' 7', ' 7+', '7-', '<7>', ' 7x' for the queries
above, ' 1,234' for to_char(1234, '9,999.'), and '100' for the numeric.sql
case. Formats without a trailing decimal point are unchanged ('9.9',
'FM9.9', '99.99PL', '99PR'). make check passes (239/239) on master
45fc51fcbf45 with the patch.
This changes the result of e.g. to_char(-7, '9.MI') from '7' to '7-'
compared with released branches. I think that is the correct result, but
you may prefer to handle the back-branches differently.
A regression test that would have caught it:
SELECT octet_length(to_char(7, '9.')), to_char(-7, '9.MI'),
to_char(-7, '9.PR');
We found this with a cross-version differential fuzzer (random to_char,
to_number and to_date calls on REL_19_STABLE vs master), written for a
university course project (CS-5187).
Regards,
Gowtham
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d)
@ 2026-10-18 20:47 Gowtham Reddy <gowthamdoma18@gmail.com>
parent: PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 5+ messages in thread
From: Gowtham Reddy @ 2026-10-18 20:47 UTC (permalink / raw)
To: gowthamdoma18@gmail.com; pgsql-bugs@lists.postgresql.org; +Cc: tgl@sss.pgh.pa.us
Hello,
Please find attached a proposed patch for BUG #NNNNN.
It stops NUM_numpart_to_char() from emitting the number string's
terminating '\0' as a digit, and adds a regression test to numeric.sql
(the test fails without the fix). make check passes (239/239) on
master 45fc51fcbf45.
Tom, CCing you since this became visible with ec352b37d.
Regards,
Gowtham
On Mon, 19 Oct 2026 at 02:11, PG Bug reporting form
<noreply@postgresql.org> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference: 19771
> Logged by: Gowtham Reddy
> Email address: gowthamdoma18@gmail.com
> PostgreSQL version: 19rc1
> Operating system: Linux x86_64
> Description:
>
> Hello,
>
> On master (tested at 45fc51fcbf45), to_char() for numbers returns a text
> value containing a NUL byte when the format has a decimal point with no
> digit positions after it:
>
> SELECT encode(textsend(to_char(7, '9.')), 'hex');
> -- master: 203700 REL_19: 2037
> SELECT encode(textsend(to_char(7, '9.PL')), 'hex');
> -- master: 2037002b REL_19: 2037
> SELECT length(to_char(7, '9.')), octet_length(to_char(7, '9.'));
> -- master: 2, 3
> SELECT to_char(7, '9.') = ' 7';
> -- master: false REL_19: true
> SELECT encode(textsend(to_char('100'::numeric, 'FM999.')), 'hex');
> -- master: 31303000
>
> The NUL byte is also written to tables: CREATE TABLE t AS SELECT
> to_char(7, '9.PL') stores 2037002b. The existing regression query in
> numeric.sql (SELECT to_char('100'::numeric, 'FM999.')) returns '100\0' on
> master but still passes, because psql stops printing at the NUL.
>
> Bisected to:
>
> ec352b37d Replace formatting.c's fixed-size output buffers with
> StringInfos.
>
> Its parent 28c5d0724 returns 2037 for the second query.
>
> The underlying problem is older. In NUM_numpart_to_char(), the "Write
> Digits" branch copies *Np->input_p even when the number string is already
> exhausted, so it copies the terminating '\0'. With the old fixed-size
> buffer that NUL ended the C string, so in released branches everything
> after the trailing decimal point is silently dropped, including the sign:
>
> SELECT to_char(-7, '9.MI'); -- REL_19: '7' (sign lost)
> SELECT to_char(-7, '9.PR'); -- REL_19: '<7' (closing bracket lost)
> SELECT to_char(7, '9.PL'); -- REL_19: ' 7' (PL lost)
> SELECT to_char(7, '9."x"'); -- REL_19: ' 7' (literal lost)
>
> With the StringInfo output buffer the NUL is kept, and the suffix appears
> after it.
>
> Proposed patch (attached, v1-0001): do not emit the string terminator as a
> digit. It also adds a regression test to numeric.sql, which fails without
> the fix. The core change is:
>
> --- a/src/backend/utils/adt/formatting.c
> +++ b/src/backend/utils/adt/formatting.c
> @@ -5659,7 +5659,7 @@ NUM_numpart_to_char(NUMProc *Np, int id)
> NUM_EMITC('0');
> }
> }
> - else
> + else if (*Np->input_p != '\0')
> {
> NUM_EMITC(*Np->input_p); /*
> Write DIGIT */
> Np->num_in = true;
>
> With this, master returns ' 7', ' 7+', '7-', '<7>', ' 7x' for the queries
> above, ' 1,234' for to_char(1234, '9,999.'), and '100' for the numeric.sql
> case. Formats without a trailing decimal point are unchanged ('9.9',
> 'FM9.9', '99.99PL', '99PR'). make check passes (239/239) on master
> 45fc51fcbf45 with the patch.
>
> This changes the result of e.g. to_char(-7, '9.MI') from '7' to '7-'
> compared with released branches. I think that is the correct result, but
> you may prefer to handle the back-branches differently.
>
> A regression test that would have caught it:
>
> SELECT octet_length(to_char(7, '9.')), to_char(-7, '9.MI'),
> to_char(-7, '9.PR');
>
> We found this with a cross-version differential fuzzer (random to_char,
> to_number and to_date calls on REL_19_STABLE vs master), written for a
> university course project (CS-5187).
>
> Regards,
> Gowtham
>
>
Attachments:
v1-0001-Fix-to_char-for-number-formats-ending-in-a-decima.patch (application/octet-stream, 3.2K) download
From b8a241af3b5db4686c7631181edb3db246754c52 Mon Sep 17 00:00:00 2001
From: Gowtham gowthamdoma18@gmail.com
Date: Mon, 19 Oct 2026 02:06:02 +0530
Subject: [PATCH v1] 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.
Before commit ec352b37d, that NUL terminated the fixed-size output
buffer early, so anything after the decimal point, including a trailing
sign, was silently dropped: to_char(-7, '9.MI') returned '7'. Since
the output became a StringInfo, the NUL is kept inside the text value
instead: to_char(7, '9.') returns ' 7\0', so length() and
octet_length() disagree and comparisons fail.
Don't emit the string terminator as a digit, and add a regression test.
The existing test of 'FM999.' did not catch this because psql stops
printing at the NUL.
---
src/backend/utils/adt/formatting.c | 2 +-
src/test/regress/expected/numeric.out | 11 +++++++++++
src/test/regress/sql/numeric.sql | 6 ++++++
3 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index 900fa8f20..132ee1700 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -5659,7 +5659,7 @@ NUM_numpart_to_char(NUMProc *Np, int id)
NUM_EMITC('0');
}
}
- else
+ else if (*Np->input_p != '\0')
{
NUM_EMITC(*Np->input_p); /* Write DIGIT */
Np->num_in = true;
diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out
index fca2cb46c..43332c3e7 100644
--- a/src/test/regress/expected/numeric.out
+++ b/src/test/regress/expected/numeric.out
@@ -2130,6 +2130,17 @@ SELECT to_char('100'::numeric, 'FM999');
100
(1 row)
+-- a trailing decimal point without digits must not add a NUL byte or
+-- swallow what follows it
+SELECT to_char(7, '9.') AS a, octet_length(to_char(7, '9.')) AS a_len,
+ to_char(-7, '9.MI') AS b, to_char(-7, '9.PR') AS c,
+ to_char(7, 'FM9.PL') AS d,
+ octet_length(to_char('100'::numeric, 'FM999.')) AS e_len;
+ a | a_len | b | c | d | e_len
+----+-------+----+-----+----+-------
+ 7 | 2 | 7- | <7> | 7+ | 3
+(1 row)
+
SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000');
to_char
-----------------
diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql
index 5fd5c02f6..380351d5c 100644
--- a/src/test/regress/sql/numeric.sql
+++ b/src/test/regress/sql/numeric.sql
@@ -1036,6 +1036,12 @@ FROM v;
SELECT to_char('100'::numeric, 'FM999.9');
SELECT to_char('100'::numeric, 'FM999.');
SELECT to_char('100'::numeric, 'FM999');
+-- a trailing decimal point without digits must not add a NUL byte or
+-- swallow what follows it
+SELECT to_char(7, '9.') AS a, octet_length(to_char(7, '9.')) AS a_len,
+ to_char(-7, '9.MI') AS b, to_char(-7, '9.PR') AS c,
+ to_char(7, 'FM9.PL') AS d,
+ octet_length(to_char('100'::numeric, 'FM999.')) AS e_len;
SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000');
SELECT to_char('100'::numeric, 'rn');
--
2.34.1
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d)
@ 2026-10-19 00:41 Tom Lane <tgl@sss.pgh.pa.us>
parent: Gowtham Reddy <gowthamdoma18@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2026-10-19 00:41 UTC (permalink / raw)
To: Gowtham Reddy <gowthamdoma18@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org
Gowtham Reddy <gowthamdoma18@gmail.com> writes:
> Please find attached a proposed patch for BUG #NNNNN.
> It stops NUM_numpart_to_char() from emitting the number string's
> terminating '\0' as a digit, and adds a regression test to numeric.sql
> (the test fails without the fix). make check passes (239/239) on
> master 45fc51fcbf45.
> Tom, CCing you since this became visible with ec352b37d.
That's definitely a bug, but on what basis do you say that it
wasn't visible before ec352b37d? AFAICT it's ancient.
I get this as far back as 9.2, for instance:
regression=# select to_char(7, 'FM99.SG') AS d;
d
---
7
(1 row)
Normally I'd think we should back-patch, but it's possible that
people didn't realize this behavior was unintended --- else we'd
have heard bug reports sooner. (It's not instantly obvious that
we should drop the decimal point but not the sign, I think.)
So I'm inclined to leave the stable branches alone. Maybe there's
still room to squeeze the fix into v19, though.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d)
@ 2026-10-20 17:03 Tom Lane <tgl@sss.pgh.pa.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2026-10-20 17:03 UTC (permalink / raw)
To: Gowtham Reddy <gowthamdoma18@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org
I wrote:
> Normally I'd think we should back-patch, but it's possible that
> people didn't realize this behavior was unintended --- else we'd
> have heard bug reports sooner. (It's not instantly obvious that
> we should drop the decimal point but not the sign, I think.)
> So I'm inclined to leave the stable branches alone. Maybe there's
> still room to squeeze the fix into v19, though.
Hearing no objections, pushed to 19 and master only.
A fair question about this is why dropping the decimal point is a good
idea at all. Maybe the actual bug is that there's not a decimal point
available in the input string. But that would be a bigger behavioral
change, and again we've heard no complaints in this area. Even if we
did change how the initial conversion works, this patch would remain a
good safety feature, so I'm content for now to do this and stop here.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d)
@ 2026-10-20 18:52 Gowtham Reddy <gowthamdoma18@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 5+ messages in thread
From: Gowtham Reddy @ 2026-10-20 18:52 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org
Hi Tom,
Thank you for committing this, and for the explanation about the
back-branches. I understand the reasoning for leaving the
decimal-point question as it is for now.
Regards, Gowtham
On Tue, 20 Oct 2026 at 22:33, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> I wrote:
> > Normally I'd think we should back-patch, but it's possible that
> > people didn't realize this behavior was unintended --- else we'd
> > have heard bug reports sooner. (It's not instantly obvious that
> > we should drop the decimal point but not the sign, I think.)
> > So I'm inclined to leave the stable branches alone. Maybe there's
> > still room to squeeze the fix into v19, though.
>
> Hearing no objections, pushed to 19 and master only.
>
> A fair question about this is why dropping the decimal point is a good
> idea at all. Maybe the actual bug is that there's not a decimal point
> available in the input string. But that would be a bigger behavioral
> change, and again we've heard no complaints in this area. Even if we
> did change how the initial conversion works, this patch would remain a
> good safety feature, so I'm content for now to do this and stop here.
>
> regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2026-10-20 18:52 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-18 20:40 BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d) PG Bug reporting form <noreply@postgresql.org>
2026-10-18 20:47 ` Gowtham Reddy <gowthamdoma18@gmail.com>
2026-10-19 00:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-20 17:03 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-20 18:52 ` Gowtham Reddy <gowthamdoma18@gmail.com>
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