agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
From: Fujii Masao <fujii@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix integer to_char() overflow with V format
Date: Tue, 01 Sep 2026 02:44:51 +0000
Message-ID: <E1x1EUU-00000002yBv-1sit@gemulon.postgresql.org> (raw)

Fix integer to_char() overflow with V format

When to_char() formatted an integer value with a V pattern, it could
return an incorrect result instead of reporting an overflow. V shifts
the decimal point by multiplying the input value by a power of ten before
formatting it, so, for example,

    to_char(3, '9V999999999')

requires computing 3 * 10^9. This result does not fit in int4, but
the integer variant of to_char() performed the multiplication using a
plain int32 expression. The intermediate result could therefore
overflow, causing the function to output incorrect digits instead of
raising "integer out of range".

Use dtoi4() and int4mul() for this calculation so that both an
out-of-range multiplier and an out-of-range product are detected, as
with ordinary integer arithmetic. This also matches the existing int8
implementation, which uses dtoi8() and int8mul() for the same
operation.

After this change, to_char() with V format either returns the
correctly formatted result when the scaled value fits in int4, or
raises "integer out of range" when it does not.

Backpatch to all supported versions.

Reported-by: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reviewed-by: MiƂosz Bieniek <bieniek.milosz@proton.me>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAB8bMivEfqZxOVdzc3kZDN++XshmkEz2t7dfGBU8+oUm864EZg@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/55af98a45aef2ed1495c6813c9d1c8ee89df3607

Modified Files
--------------
src/backend/utils/adt/formatting.c | 16 +++++++++-------
src/test/regress/expected/int4.out | 27 +++++++++++++++++++++++++++
src/test/regress/sql/int4.sql      |  9 +++++++++
3 files changed, 45 insertions(+), 7 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1x1EUU-00000002yBv-1sit@gemulon.postgresql.org>
Permalink:  ../E1x1EUU-00000002yBv-1sit@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1x1EUU-00000002yBv-1sit@gemulon.postgresql.org

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: fujii@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix integer to_char() overflow with V format
  In-Reply-To: <E1x1EUU-00000002yBv-1sit@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox