Обсуждение: BUG #17167: UndefinedBehaviorSanitizer: invalid-shift-exponent while running int4shr/int4shl

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

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

От
PG Bug reporting form
Дата:
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


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

От
Alexander Lakhin
Дата:
Hello Subhrajyoti,

30.08.2021 12:02, PG Bug reporting form wrote:
> Bug reference:      17167
>
> 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 like a duplicate of #16790:
https://www.postgresql.org/message-id/flat/16790-c2a81c92a8776c5b%40postgresql.org

Best regards,
Alexander