QUERY · ISSUE
Overflow converting -2147483648 to int when using MPZ
py-core
When MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ, calling mp_obj_get_int() for a Python variable that contains the value -2147483648 results in an overflow converting long int to machine word error.
If MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG instead, the conversion works OK.
Is the MPZ overflow check incorrect?
CANDIDATE · PULL REQUEST
mpz_hash: avoid undefined behavior at integer overflow
Before this, ubsan would detect a problem when executing
hash(006699999999999999999999999999999999999999999999999999999999999999999999):
../../py/mpz.c:1539:20: runtime error: left shift of 1067371580458 by 32 places cannot be represented in type 'mp_int_t' (aka 'long')
When the overflow does occur, it now happens as defined by the rules of
unsigned arithmetic.