Re: [PATCH] intXshr, intXshl: return error on shift count out of range

Поиск
Список
Период
Сортировка
Искать

Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
Pavel Borisov <pashkin.elfe@gmail.com>
Дата:
Hi, Egor!

On Wed, 30 Sept 2026 at 18:03, Egor Ivkov  wrote:
>
> Small fix for .out files to contain rightmost spaces.
>
> Regards,
> Egor Ivkov

Your patch looks good to me except the similar behavior of int2 and
int4 are never explained. In C11 and older int16  left arg bit shift
is not UB only if the result filts int32. There is no check for this
in the patch.

Regards,
Pavel Borisov
Supabase


Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
Alexander Lakhin <exclusion@gmail.com>
Дата:
Hello Egor and Pavel,

30.09.2026 17:28, Pavel Borisov wrote:
Small fix for .out files to contain rightmost spaces.

Regards,
Egor Ivkov
Your patch looks good to me except the similar behavior of int2 and
int4 are never explained. In C11 and older int16  left arg bit shift
is not UB only if the result filts int32. There is no check for this
in the patch.

FWIW, this was already reported several times:
[1] https://www.postgresql.org/message-id/48099055.9040607%40dls.net
[2] https://www.postgresql.org/message-id/16790-c2a81c92a8776c5b%40postgresql.org
[3] https://www.postgresql.org/message-id/17167-028026e4ca333817%40postgresql.org
[4] https://www.postgresql.org/message-id/19672-33e7d9d1be2229ea%40postgresql.org


By the way, having a RISC-V machine handy:
Linux orangepirv2 6.6.63-ky #1.0.0 SMP PREEMPT Wed Mar 12 09:04:00 CST 2025 riscv64 riscv64 riscv64 GNU/Linux
I've tried:
SELECT int4shl(1, 100);
 int4shl
---------
      16
(1 row)

Best regards,
Alexander

Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Alexander Lakhin  writes:
> By the way, having a RISC-V machine handy:
> Linux orangepirv2 6.6.63-ky #1.0.0 SMP PREEMPT Wed Mar 12 09:04:00 CST 2025 riscv64 riscv64 riscv64 GNU/Linux
> I've tried:
> SELECT int4shl(1, 100);
>   int4shl
> ---------
>        16
> (1 row)

FWIW, I don't agree with the premise of this patch, even a little bit.
To my mind, the purpose of these functions and their siblings is to
provide access to the C-level bitwise operators, which will do
whatever they do on your platform.  There is no contract to restrict
them to some guaranteed-portable functionality subset, and I think
trying to do that would accomplish little except to break code that
had been working fine in the context it's used in.

We have generally taken a similar approach with respect to other
things that are platform-dependent, such as floating-point math.
FP math is more portable than it used to be thanks to IEEE 754's
achievement of world domination; but there are still discrepancies,
and we don't typically try to hide them.

There might be room for a documentation patch that adds something
like

-Bitwise shift left
+Bitwise shift left (defined to act like the C << operator)

just to clarify our intent.

			regards, tom lane


Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
On Thu, 1 Oct 2026 at 08:25, Tom Lane  wrote:
> FWIW, I don't agree with the premise of this patch, even a little bit.

I'm inclined to agree here. Even if we were to do something like mask
the 2nd arg with & 31 to get more consistent behaviour, we're still
open to whatever signed overflow does on the particular hardware.

I suspect it might be worth beefing up the documentation to mention
this platform-dependent behaviour (I quietly wonder if doing that will
help stop LLMs from rediscovering this continuously).

David


Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
David Rowley  writes:
> I suspect it might be worth beefing up the documentation to mention
> this platform-dependent behaviour (I quietly wonder if doing that will
> help stop LLMs from rediscovering this continuously).

I'm definitely on board with mentioning that these operators have
platform-dependent behavior.  I doubt we should try to enumerate
any details.

			regards, tom lane


Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
Вложения:
On Thu, 1 Oct 2026 at 11:58, Tom Lane  wrote:
>
> David Rowley  writes:
> > I suspect it might be worth beefing up the documentation to mention
> > this platform-dependent behaviour (I quietly wonder if doing that will
> > help stop LLMs from rediscovering this continuously).
>
> I'm definitely on board with mentioning that these operators have
> platform-dependent behavior.  I doubt we should try to enumerate
> any details.

Here's an attempt at that.

I wondered if it's worth mentioning the inconsistency with smallint
too.  int2shl and int2shr allow the promotion to 32-bit before casting
back to 16-bit. That might surprise a few people. Consider:

select 1::smallint << 16, 1::int << 32, 1::bigint << 64;;
 ?column? | ?column? | ?column?
----------+----------+----------
        0 |        1 |        1
(1 row)

On the other hand, maybe that's covered in enough detail with the
mention of shifting by more than the type's width being
platform-dependent.

David

Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
David Rowley  writes:
> On Thu, 1 Oct 2026 at 11:58, Tom Lane  wrote:
>> I'm definitely on board with mentioning that these operators have
>> platform-dependent behavior.  I doubt we should try to enumerate
>> any details.

> Here's an attempt at that.

Perhaps s/in terms of/in cases of/, or similar wording.
Otherwise I'm okay with it.

			regards, tom lane


Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
On Thu, 1 Oct 2026 at 13:33, Tom Lane  wrote:
>
> David Rowley  writes:
> > On Thu, 1 Oct 2026 at 11:58, Tom Lane  wrote:
> >> I'm definitely on board with mentioning that these operators have
> >> platform-dependent behavior.  I doubt we should try to enumerate
> >> any details.
>
> > Here's an attempt at that.
>
> Perhaps s/in terms of/in cases of/, or similar wording.
> Otherwise I'm okay with it.

OK, done that way. Thanks.

David


Re: [PATCH] intXshr, intXshl: return error on shift count out of range

От:
Pavel Borisov <pashkin.elfe@gmail.com>
Дата:
Hi!

While I think that OP's patch is close to being correct, I also agree
with Tom and Alexander that the scope of this UB is outside of
Postgres code. So I fully agree with the final approach. Thanks to
everyone in this thread!

Regards,
Pavel Borisov


FAQ