Re: 64-bit integer subtraction bug on some platforms

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: 64-bit integer subtraction bug on some platforms
Date: 2023-11-08 12:15:35
Message-ID: 2fb325a05184d44f601a3fca0117dd3e04f07eba.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 2023-11-08 at 11:58 +0000, Dean Rasheed wrote:
> One of the new tests in the infinite interval patch has revealed a bug
> in our 64-bit integer subtraction code. Consider the following:
>
> select 0::int8 - '-9223372036854775808'::int8;
>
> This should overflow, since the correct result (+9223372036854775808)
> is out of range. However, on platforms without integer overflow
> builtins or 128-bit integers, pg_sub_s64_overflow() does the
> following:
>
> if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) ||
> (a > 0 && b < 0 && a > PG_INT64_MAX + b))
> {
> *result = 0x5EED; /* to avoid spurious warnings */
> return true;
> }
> *result = a - b;
> return false;
>
> which fails to spot the fact that overflow is also possible when a ==
> 0. So on such platforms, it returns the wrong result.
>
> Patch attached.

The patch looks good to me.

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Laurenz Albe 2023-11-08 12:23:28 Re: Fix output of zero privileges in psql
Previous Message Daniel Gustafsson 2023-11-08 12:15:00 Re: pgsql: Clean up role created in new subscription test.