-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
py/objint_longlong: Fix overflow check in mp_obj_int_get_checked. #17739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
py/objint_longlong: Fix overflow check in mp_obj_int_get_checked. #17739
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #17739 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 171 171
Lines 22239 22239
=======================================
Hits 21880 21880
Misses 359 359 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code size report:
|
@dpgeorge Can you help me understand what could cause zephir port to cause a syntax error when compiling |
I think it's because the zephyr minimal build doesn't enable slicing, ie I suggest finding another way to test it, that doesn't use slicing. |
fc5cbad
to
b175b45
Compare
Thank you, I forgot that it was even possible to make a build without slicing support... |
print('unhandled overflow') | ||
except OverflowError as e: | ||
print('ok') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth adjusting this test to make it a bit clearer what is going on, eg:
x = 1 << 32
r = None
try:
r = range(0, x)
except OverflowError:
# 32-bit target, correctly handled the overflow of x
print("ok")
if r is not None:
if len(r) == x:
# 64-bit target, everything is just a small-int
print("ok")
else:
# 32-bit target that did not handle the overflow of x
print("unhandled overflow")
This is to fix an outstanding TODO. The test cases is using a range as this will exist in all builds, but mp_obj_get_int is used in many different parts of code where an overflow is more likely to occur. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
b175b45
to
2d119b1
Compare
Makes sense, this is much easier to understand. I just pushed the updated code. |
Note: the CI failure is unrelated to this PR: --- /home/runner/work/micropython/micropython/tests/results/thread_thread_gc1.py.exp 2025-07-24 08:53:02.717730694 +0000
+++ /home/runner/work/micropython/micropython/tests/results/thread_thread_gc1.py.out 2025-07-24 08:53:02.717730694 +0000
@@ -1 +1,2 @@
-True
+qemu: uncaught target signal 11 (Segmentation fault) - core dumped |
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.