BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl
Date
Msg-id 17167-028026e4ca333817@postgresql.org
Whole thread Raw
Responses Re: BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl  (Alexander Lakhin <exclusion@gmail.com>)
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      17167
Logged by:          Subhrajyoti Senapati
Email address:      ssubhrajyoti@google.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


pgsql-bugs by date:

Previous
From: "David G. Johnston"
Date:
Subject: BUG #17166: PREPARE without types inconsistent type resolving
Next
From: Alexander Lakhin
Date:
Subject: Re: BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl