to_char() on integer with V format silently wraps on overflow

From: Andrey Rachitskiy <pl0h0yp1(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: to_char() on integer with V format silently wraps on overflow
Date: 2026-08-13 12:01:02
Message-ID: CAB8bMivEfqZxOVdzc3kZDN++XshmkEz2t7dfGBU8+oUm864EZg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi, All!

to_char() with a V format multiplies the value by 10^n. For integer
input that multiply is a bare int32 expression. On overflow the
product wraps and to_char() prints the wrapped digits. There is no
error. The same call on bigint is correct. Plain integer multiply
already raises "integer out of range".

SELECT to_char(3, '9V999999999');
-- actual: -1294967296
-- expected: ERROR: integer out of range

SELECT to_char(2147483647, '9V9');
-- actual: -10
-- expected: ERROR: integer out of range

SELECT to_char(3::bigint, '9V999999999');
-- 3000000000

SELECT 3 * 1000000000;
-- ERROR: integer out of range

Cause: int4_to_char() does value * (int32) pow(10, multi) with no
overflow check. int8_to_char() already uses int8mul and dtoi8.

The attached patch uses int4mul and dtoi4 for the same step with regress
tests.

Thoughts?

--
Regards,
Rachitskiy Andrey

Attachment Content-Type Size
0001-Fix-int4-to-char-V-multi-overflow-check.patch text/x-patch 3.2 KB

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Banck 2026-08-13 12:37:20 Re: pg_upgrade from 17.10 to 18.4 on Ubuntu fails: Finding the real data directory for the target cluster sh: 1: (null)/postgres: not found
Previous Message PG Bug reporting form 2026-08-13 10:47:17 BUG #19618: postgresql18 client package not found