to_char() on integer with V format silently wraps on overflow
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
Hi Andrey
Nice catch! I successfully ran all the tests locally.
LGTM!
Kind regards,
Miłosz Bieniek
On Thu, Aug 27, 2026 at 5:57 PM Miłosz Bieniek <bieniek.milosz@proton.me> wrote:
Hi Andrey
Nice catch! I successfully ran all the tests locally.
LGTM!
The patch looks good to me, too.
Barring any objections, I'll commit it.
Regards,
--
Fujii Masao
чт, 27 авг. 2026 г. в 15:30, Fujii Masao <masao.fujii@gmail.com>:
On Thu, Aug 27, 2026 at 5:57 PM Miłosz Bieniek
Nice catch! I successfully ran all the tests locally.
LGTM!The patch looks good to me, too.
Barring any objections, I'll commit it.Dear Miłosz and Fujii-san,
Thanks for the review.
--
Regards,
Rachitskiy Andrey