pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #16790: Integer overflow not detected with <<
3+ messages / 3 participants
[nested] [flat]

* BUG #16790: Integer overflow not detected with <<
@ 2020-12-23 09:00 PG Bug reporting form <noreply@postgresql.org>
  2020-12-23 15:29 ` Re: BUG #16790: Integer overflow not detected with << Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: PG Bug reporting form @ 2020-12-23 09:00 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: exclusion@gmail.com

The following bug has been logged on the website:

Bug reference:      16790
Logged by:          Alexander Lakhin
Email address:      exclusion@gmail.com
PostgreSQL version: 13.1
Operating system:   Ubuntu 20.04
Description:        

It seems that the bitwise shift left operator ignores overflow:
SELECT -1::int4<<32;
-1
But:
SELECT (-1::int4<<31) * 2;
ERROR:  integer out of range

Is this an expected behavior (a kind of UB)? It's not like an arithmetic
shift. (The semantic of '1::int4<<-1' is questionable too.)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #16790: Integer overflow not detected with <<
  2020-12-23 09:00 BUG #16790: Integer overflow not detected with << PG Bug reporting form <noreply@postgresql.org>
@ 2020-12-23 15:29 ` Tom Lane <tgl@sss.pgh.pa.us>
  2020-12-23 17:50   ` Re: BUG #16790: Integer overflow not detected with << Alexander Lakhin <exclusion@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2020-12-23 15:29 UTC (permalink / raw)
  To: exclusion@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org

PG Bug reporting form <noreply@postgresql.org> writes:
> It seems that the bitwise shift left operator ignores overflow:
> Is this an expected behavior (a kind of UB)? It's not like an arithmetic
> shift. (The semantic of '1::int4<<-1' is questionable too.)

It's defined to do whatever the C << operator does.  That will certainly
not involve throwing an error.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #16790: Integer overflow not detected with <<
  2020-12-23 09:00 BUG #16790: Integer overflow not detected with << PG Bug reporting form <noreply@postgresql.org>
  2020-12-23 15:29 ` Re: BUG #16790: Integer overflow not detected with << Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-12-23 17:50   ` Alexander Lakhin <exclusion@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Alexander Lakhin @ 2020-12-23 17:50 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org

23.12.2020 18:29, Tom Lane wrote:
> PG Bug reporting form <noreply@postgresql.org> writes:
>> It seems that the bitwise shift left operator ignores overflow:
>> Is this an expected behavior (a kind of UB)? It's not like an arithmetic
>> shift. (The semantic of '1::int4<<-1' is questionable too.)
> It's defined to do whatever the C << operator does.  That will certainly
> not involve throwing an error.
Unfortunately, the behavior of the C << operator varies from defined to
implementation-defined and undefined. In fact I found this issue when
executing that query in an instance built with "-fsanitize=undefined".
Also I found an interesting reading about the shift operator:
https://devblogs.microsoft.com/cppblog/hello-arm-exploring-undefined-unspecified-and-implementation-...
So it's not that simple to describe the expected result of the operator
for all cases/platforms/architectures. It seems that a user is left with
"just try and see" or "avoid illegal values" approach.
On the other hand, there are pg_{mul,add,sub}_s{16,32,64}_overflow()
functions. Aren't they purposed to provide a well-defined behaviour for
related arithmetic operators?
Maybe the bitwise shift can be thought as something different from
arithmetic shift, and i<<1 is not the same as i*2, but in my opinion
it's not that could be expected at the database level.

Best regards,
Alexander





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2020-12-23 17:50 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-12-23 09:00 BUG #16790: Integer overflow not detected with << PG Bug reporting form <noreply@postgresql.org>
2020-12-23 15:29 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-12-23 17:50   ` Alexander Lakhin <exclusion@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox