Skip to content

vfs: Check that open() resulted in a file-like object. #17868

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Aug 8, 2025

Summary

Check that open() results in a file-like object, preventing crashes in callers to mp_vfs_open when an object that is "not actually a file" is returned. This includes, for example, execfile.

Closes: #17841

Testing

I added a test derived from the reproducer in #17841.

Trade-offs and Alternatives

The code can only ensure that the object's type defines a protocol slot. Due to protocol confusion, a variant of the original crasher that returned e.g., a machine.Pin instance could still lead to a crash. (#17852)

That is, an object whose type defines the protocol slot.

Note that due to protocol confusion, a variant of the original
crasher that returned e.g., a machine.Pin instance could still
lead to a crash. (micropython#17852)

Closes: micropython#17841

Signed-off-by: Jeff Epler <jepler@gmail.com>
Copy link

github-actions bot commented Aug 8, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   +56 +0.007% standard
      stm32:   +16 +0.004% PYBV10
     mimxrt:   +24 +0.006% TEENSY40
        rp2:   +24 +0.003% RPI_PICO_W
       samd:   +16 +0.006% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +32 +0.007% VIRT_RV32

@jepler
Copy link
Contributor Author

jepler commented Aug 8, 2025

Multiple tests fail because their vfs open method returns None (or other unsuitable objects, I didn't look any further than the first one).

Should the tests be corrected so they return a minimal file-like object, or should I take a different approach to resolving the crash in execfile()?

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

execfile: segmentation fault with incorrect Python vfs implementation
2 participants