BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: ssubhrajyoti(at)google(dot)com
Subject: BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl
Date: 2021-08-30 09:02:36
Message-ID: 17167-028026e4ca333817@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 17167
Logged by: Subhrajyoti Senapati
Email address: ssubhrajyoti(at)google(dot)com
PostgreSQL version: 12.8
Operating system: Linux
Description:

Hi,

When executing in an asan instrumented build,
```
postgres=> SELECT int4shr(10, -1);
```
throws an error `runtime error: shift exponent 32 is too large for 32-bit
type 'int32' (aka 'int')`

This seems to be coming from this line:
https://github.com/postgres/postgres/blob/c30f54ad732ca5c8762bb68bbe0f51de9137dd72/src/backend/utils/adt/int.c#L1413

While browsing, i found this link:
https://wiki.sei.cmu.edu/confluence/display/c/INT34-C.+Do+not+shift+an+expression+by+a+negative+number+of+bits+or+by+greater+than+or+equal+to+the+number+of+bits+that+exist+in+the+operand,
according to which shifting by negative number of bits or >= bits in the
operand is classified as a undefined behaviour.

Few experiments i tried:
```
postgres=> SELECT int4shr(10, 1);
int4shr
---------
5
(1 row)

postgres=> SELECT int4shr(10, -1);
int4shr
---------
0
(1 row)

postgres=> SELECT int4shr(10, -2);
int4shr
---------
0
(1 row)
```

I am not sure if it's a bug or expected behaviour.

Thanks,
Subhrajyoti

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Alexander Lakhin 2021-08-30 12:00:51 Re: BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl
Previous Message David G. Johnston 2021-08-29 18:44:39 BUG #17166: PREPARE without types inconsistent type resolving