Fix BUG #19586: money division overflow for INT64_MIN / -1

Started by Andrey Rachitskiy4 months ago3 messageshackers
Jump to latest
#1Andrey Rachitskiy
pl0h0yp1@gmail.com

Hi, Hackers!

This continues BUG #19586 (money division overflow):
/messages/by-id/19586-bb603bf5ad9934dd@postgresql.org

cash_div_int64() rejects division by zero, but not INT64_MIN / -1.
On x86-64 that surfaces as a floating-point exception via SIGFPE; on
aarch64 it silently returns INT64_MIN. Multiplication by -1 is already
guarded (commit 4f96281587).
The attached patch handles division by -1 as negation, matching
int8div(), and raises "money out of range" when the dividend is
PG_INT64_MIN. Regression tests cover int8/int4/int2.

Tristan looked at the patch on -bugs and suggested posting it here.

Patch attached.

Attachments:

t253247_1
0001-Fix-money-div-INT64_MIN-by-minus-one-overflow.patchtext/x-patch; charset=US-ASCII; name=0001-Fix-money-div-INT64_MIN-by-minus-one-overflow.patchDownload+28-1
#2Rafia Sabih
rafia.pghackers@gmail.com
In reply to: Andrey Rachitskiy (#1)
Re: Fix BUG #19586: money division overflow for INT64_MIN / -1

On Thu, 30 Jul 2026 at 11:01, Andrey Rachitskiy <pl0h0yp1@gmail.com> wrote:

Hi, Hackers!

This continues BUG #19586 (money division overflow):
/messages/by-id/19586-bb603bf5ad9934dd@postgresql.org

cash_div_int64() rejects division by zero, but not INT64_MIN / -1.
On x86-64 that surfaces as a floating-point exception via SIGFPE; on
aarch64 it silently returns INT64_MIN. Multiplication by -1 is already
guarded (commit 4f96281587).
The attached patch handles division by -1 as negation, matching
int8div(), and raises "money out of range" when the dividend is
PG_INT64_MIN. Regression tests cover int8/int4/int2.

Tristan looked at the patch on -bugs and suggested posting it here.

Patch attached.

I looked into this patch and it looks clean and complete as per the
requirements mentioned.
I don't find any commitfest entry for this, maybe you can add it to get
timely attention to this.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

#3David Rowley
dgrowleyml@gmail.com
In reply to: Andrey Rachitskiy (#1)
Re: Fix BUG #19586: money division overflow for INT64_MIN / -1

On Thu, 30 Jul 2026 at 17:31, Andrey Rachitskiy <pl0h0yp1@gmail.com> wrote:

This continues BUG #19586 (money division overflow):
/messages/by-id/19586-bb603bf5ad9934dd@postgresql.org

Thanks. Pushed.

David