Re: Remove dependence on integer wrapping

From: Joseph Koshakow <koshy44(at)gmail(dot)com>
To: Alexander Lakhin <exclusion(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Andres Freund <andres(at)anarazel(dot)de>, Nathan Bossart <nathandbossart(at)gmail(dot)com>
Subject: Re: Remove dependence on integer wrapping
Date: 2024-06-14 02:48:14
Message-ID: CAAvxfHfqJca-WGvWJQ3ZAtt2po=mkQzU8r9Qjzom4YqxQi84oA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Jun 13, 2024 at 12:00 AM Alexander Lakhin <exclusion(at)gmail(dot)com>
wrote:
>
> Let me remind you of bug #18240. Yes, that was about float8, but with
> -ftrapv we can get into the trap with:
> SELECT 1_000_000_000::money * 1_000_000_000::int;
> server closed the connection unexpectedly

Interesting, it looks like there's no overflow handling of any money
arithmetic. I've attached
v4-0002-Handle-overflow-in-money-arithmetic.patch which adds some
overflow checks and tests. I didn't address the float multiplication
because I didn't see any helper methods in int.h. I did some some
useful helpers in float.h, but they raise an error directly instead
of returning a bool. Would those be appropriate for use with the
money type? If not I can refactor out the inner parts into a new method
that returns a bool.

v4-0001-Remove-dependence-on-integer-wrapping.patch is unchanged, I
just incremented the version number.

> Also there are several trap-producing cases with date types:
> SELECT to_date('100000000', 'CC');
> SELECT to_timestamp('1000000000,999', 'Y,YYY');
> SELECT make_date(-2147483648, 1, 1);
>
> And one more with array...
> CREATE TABLE t (ia int[]);
> INSERT INTO t(ia[2147483647:2147483647]) VALUES ('{}');

I'll try and get patches to address these too in the next couple of
weeks unless someone beats me to it.

> I think it's not the whole iceberg too.

+1

Thanks,
Joe Koshakow

Attachment Content-Type Size
v4-0001-Remove-dependence-on-integer-wrapping.patch text/x-patch 13.5 KB
v4-0002-Handle-overflow-in-money-arithmetic.patch text/x-patch 5.5 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Joseph Koshakow 2024-06-14 02:56:38 Re: Remove dependence on integer wrapping
Previous Message Noah Misch 2024-06-14 02:42:25 Re: race condition in pg_class