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

Started by Andrey Rachitskiy28 days ago5 messagesbugs
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

This thread has been committed, so CI has stopped here. Anything below is the last result it produced.

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t253399
psql -h localhost -U postgres

Built from patchset v1 (message #1), August 31, 2026 at 01:18 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t253399_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t253399_1 && git checkout t253399_1

Patchset v1 (message #1) is on t253399_1

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