← index #6332PR #17739
Related · high · value 0.257
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

py/objint_longlong: Fix overflow check in mp_obj_int_get_checked.

mergedby yoctopuceopened 2025-07-22updated 2025-07-28
py-core

Summary

This is to fix an outstanding TODO. The test cases is using a range expression, as this is available all builds, but mp_obj_get_int is used by various extmod where an overflow is more likely to occur.

Testing

The code is pretty simple, and has been tested on unix/longlong varient.
An test case has been added in CI tests.

Trade-offs and Alternatives

I am not sure that this exception is likely to occur in the MicroPython core code ,unless the Python code includes a serious bug. Therefore, the extra code size might not be worthwhile for everyone. However, there are many hardware extensions that use this function, and in those cases, the likelihood of an overflow is much higher.

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