← index #6332PR #3802
Related · high · value 3.688
QUERY · ISSUE

Overflow converting -2147483648 to int when using MPZ

openby markstzopened 2020-08-12updated 2020-08-12
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

closedby jepleropened 2018-05-19updated 2018-05-21

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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied