[PATCH] intXshr, intXshl: return error on shift count out of range

From: Egor Ivkov <e(dot)ivkov(at)arenadata(dot)io>
To: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Ilya Khaprov <i(dot)khaprov(at)arenadata(dot)io>
Subject: [PATCH] intXshr, intXshl: return error on shift count out of range
Date: 2026-09-29 21:47:21
Message-ID: 180711790717647@mail.360.yandex.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Current behavior of int4shr/int4shl/.. is an UB in cases where shift count exceeds bitness of the integer being shifted.

 

For example in this case if `arg2` >= 32 this is an UB:

 


Datum


int4shl(PG_FUNCTION_ARGS)


{


    int32        arg1 = PG_GETARG_INT32(0);


    int32        arg2 = PG_GETARG_INT32(1);


 


    PG_RETURN_INT32(arg1 << arg2);


}

 

x86 handled this by applying a mask (e.g. & 31) while ARM will produce 0 and RISC-V will crash. So besides being an UB it is inconsistent across platforms.


 


The proposed solution is to report an error to the user: "shift count out of range". This is also what other functions already do, for example:


int4pl/int4mi/int4mul/int4neg report ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE, "integer out of range" (int.c:825–856)


The corresponding patch file is attached.

Originally the issue was found by fuzzing by Ilya Khaprov.

Attachment Content-Type Size
unknown_filename text/html 1.3 KB
return-error-on-shift-count-out-of-range-v1.patch text/x-diff 6.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Vik Fearing 2026-09-29 21:51:39 Re: Logical Implication
Previous Message Andres Freund 2026-09-29 21:44:57 Re: BUG #19686: Rolling back SET TABLESPACE