Skip to content

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

Merged
merged 1 commit into from
Jul 28, 2025

Conversation

yoctopuce
Copy link
Contributor

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.

Copy link

codecov bot commented Jul 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.38%. Comparing base (062e82a) to head (3c69277).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17739   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files         171      171           
  Lines       22240    22240           
=======================================
  Hits        21881    21881           
  Misses        359      359           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@yoctopuce
Copy link
Contributor Author

@dpgeorge Can you help me understand what could cause zephir port to cause a syntax error when compiling int_64_basics.py ?
I have the impression that the new code is super safe... Could it be a problem with constant folding of the expression 1 << 32 ?

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 23, 2025
@dpgeorge
Copy link
Member

Can you help me understand what could cause zephir port to cause a syntax error when compiling int_64_basics.py ?

I think it's because the zephyr minimal build doesn't enable slicing, ie a[:].

I suggest finding another way to test it, that doesn't use slicing.

@yoctopuce yoctopuce force-pushed the fix_longlong_int_get_checked branch from fc5cbad to b175b45 Compare July 24, 2025 07:07
@yoctopuce
Copy link
Contributor Author

Thank you, I forgot that it was even possible to make a build without slicing support...
I have updated the test code using a plain range(), that should do the trick.

print('unhandled overflow')
except OverflowError as e:
print('ok')

Copy link
Member

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")

@yoctopuce yoctopuce force-pushed the fix_longlong_int_get_checked branch from b175b45 to 2d119b1 Compare July 24, 2025 08:47
@yoctopuce
Copy link
Contributor Author

Makes sense, this is much easier to understand. I just pushed the updated code.

@yoctopuce
Copy link
Contributor Author

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

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>
@dpgeorge dpgeorge force-pushed the fix_longlong_int_get_checked branch from 2d119b1 to 3c69277 Compare July 28, 2025 14:31
@dpgeorge dpgeorge merged commit 3c69277 into micropython:master Jul 28, 2025
70 checks passed
@yoctopuce yoctopuce deleted the fix_longlong_int_get_checked branch July 28, 2025 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants