Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xIqHG-00000003va6-2Er7 for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Oct 2026 16:31:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xIqHF-000000087aZ-0kaL for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Oct 2026 16:31:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xIXgp-00000004B7g-3WL3 for pgsql-bugs@lists.postgresql.org; Sun, 18 Oct 2026 20:41:07 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xIXgn-00000002h7S-34y6 for pgsql-bugs@lists.postgresql.org; Sun, 18 Oct 2026 20:41:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=LzLHqo8oDL1W4nWe+pPZZEE+0mMAD3+yTdPzJVjmlAo=; b=Qwzvz2JtTkQltyJu25J4LFan6e 6yskll78tlqUWfsnQNkJyAqZfHi0IjbrI1Cjz/Ryhe/jsBh3UX14KRTmRm9D+0S44m2N6Ion15fq3 Y0hspwlYcBo/XwKvMssWsPAWOXtKt89ZMzUhq0QavOO99IHL2HCV4inERzs2/TFhOXBt25yhQ7EK2 Y1dv9v3K2TypQ8D8yRFwSk2BZM0Gxc2bArWqJoKwIIKTAtAr/HRA8a2C8xRWIhmoWj1BkVk1Qw75g mEsqYZWchJgiUJHUBQVUbe8V0HXbpL7KL1JjY088G0kL3aHTqiEsWUsbgwikX6Zk0Y4Q2srincx1Y /DFbx+kg==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xIXgn-00000007q2i-1PAY for pgsql-bugs@lists.postgresql.org; Sun, 18 Oct 2026 20:41:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xIXgl-000000065Fc-2aqT for pgsql-bugs@lists.postgresql.org; Sun, 18 Oct 2026 20:41:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19771: to_char(): NUL byte in result for formats with a trailing decimal point (regression from ec352b37d) To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: gowthamdoma18@gmail.com Reply-To: gowthamdoma18@gmail.com, pgsql-bugs@lists.postgresql.org Date: Sun, 18 Oct 2026 20:40:57 +0000 Message-ID: <19771-4f173b891525020c@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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: =20 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.') =3D ' 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 !=3D '\0') { NUM_EMITC(*Np->input_p); /* Write DIGIT */ Np->num_in =3D 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