Skip to content

Commit

Permalink
extmod/vfs_posix_file: Ensure file object has safe default fd.
Browse files Browse the repository at this point in the history
With this commit, if file open fails, the object will have fd = -1 (closed)
and the finaliser will not attempt to close anything.

Fixes issue micropython#13672.

Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
pi-anl authored and dpgeorge committed Feb 20, 2024
1 parent 31e131b commit 2962e24
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions extmod/vfs_posix_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_
}

mp_obj_vfs_posix_file_t *o = mp_obj_malloc_with_finaliser(mp_obj_vfs_posix_file_t, type);
o->fd = -1; // In case open() fails below, initialise this as a "closed" file object.

mp_obj_t fid = file_in;

Expand Down

0 comments on commit 2962e24

Please sign in to comment.