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

Started by Andrey Rachitskiy3 months ago5 messagesbugs
Jump to latest
#1Andrey Rachitskiy
pl0h0yp1@gmail.com

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

Attachments:

t253399_1
0001-Fix-int4-to-char-V-multi-overflow-check.patchtext/x-patch; charset=US-ASCII; name=0001-Fix-int4-to-char-V-multi-overflow-check.patchDownload+45-7
#2Miłosz Bieniek
bieniek.milosz@proton.me
In reply to: Andrey Rachitskiy (#1)
Re: to_char() on integer with V format silently wraps on overflow

Hi Andrey

Nice catch! I successfully ran all the tests locally.
LGTM!

Kind regards,
Miłosz Bieniek

#3Fujii Masao
masao.fujii@gmail.com
In reply to: Miłosz Bieniek (#2)
Re: to_char() on integer with V format silently wraps on overflow

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

#4Andrey Rachitskiy
pl0h0yp1@gmail.com
In reply to: Fujii Masao (#3)
Re: to_char() on integer with V format silently wraps on overflow

чт, 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

#5Fujii Masao
masao.fujii@gmail.com
In reply to: Andrey Rachitskiy (#4)
Re: to_char() on integer with V format silently wraps on overflow

On Fri, Aug 28, 2026 at 6:27 AM Andrey Rachitskiy <pl0h0yp1@gmail.com> wrote:

Thanks for the review.

I've pushed the patch. Thanks!

Regards,

--
Fujii Masao