pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Lakhin <exclusion@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16790: Integer overflow not detected with <<
Date: Wed, 23 Dec 2020 20:50:00 +0300
Message-ID: <17340682-ad3e-9145-543a-27a62518357e@gmail.com> (raw)
In-Reply-To: <710063.1608737396@sss.pgh.pa.us>
References: <16790-c2a81c92a8776c5b@postgresql.org>
	<710063.1608737396@sss.pgh.pa.us>

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





view thread (3+ messages)

Message-ID: <17340682-ad3e-9145-543a-27a62518357e@gmail.com>
Permalink:  ../17340682-ad3e-9145-543a-27a62518357e@gmail.com/
Also on:    postgresql.org/message-id/17340682-ad3e-9145-543a-27a62518357e@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: exclusion@gmail.com, tgl@sss.pgh.pa.us, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #16790: Integer overflow not detected with <<
  In-Reply-To: <17340682-ad3e-9145-543a-27a62518357e@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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