extmod/vfs_blockdev: Check return type is an integer.

This catches cases where the return type is not a small int, eg it could be
a string, or even a big integer.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2025-09-16 13:15:10 +10:00
parent 6d65882b05
commit 7681c68392
3 changed files with 17 additions and 17 deletions

View File

@@ -69,7 +69,7 @@ static int mp_vfs_blockdev_call_rw(mp_obj_t *args, size_t block_num, size_t bloc
// and negative integer on errors. Check for positive integer
// results as some callers (i.e. littlefs) will produce corrupt
// results from these.
int i = MP_OBJ_SMALL_INT_VALUE(ret);
int i = mp_obj_get_int(ret);
return i > 0 ? (-MP_EINVAL) : i;
}
}

View File

@@ -70,8 +70,8 @@ def test(vfs_class):
try:
with fs.open("test", "r") as f:
print("opened")
except OSError as e:
print("OSError", e)
except Exception as e:
print(type(e), e)
# This variant should succeed on open, may fail on read
# unless the filesystem cached the contents already
@@ -81,8 +81,8 @@ def test(vfs_class):
bdev.read_res = res
print("read 1", f.read(1))
print("read rest", f.read())
except OSError as e:
print("OSError", e)
except Exception as e:
print(type(e), e)
test(vfs.VfsLfs2)

View File

@@ -2,27 +2,27 @@
opened
read 1 a
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OSError [Errno 5] EIO
<class 'OSError'> [Errno 5] EIO
read 1 a
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OSError [Errno 22] EINVAL
<class 'OSError'> [Errno 22] EINVAL
read 1 a
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OSError [Errno 22] EINVAL
<class 'OSError'> [Errno 22] EINVAL
read 1 a
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OSError [Errno 22] EINVAL
<class 'TypeError'> can't convert str to int
read 1 a
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<class 'VfsFat'>
opened
read 1 a
read rest aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OSError [Errno 5] EIO
OSError [Errno 5] EIO
OSError [Errno 5] EIO
OSError [Errno 5] EIO
OSError [Errno 5] EIO
OSError [Errno 5] EIO
OSError [Errno 5] EIO
OSError [Errno 5] EIO
<class 'OSError'> [Errno 5] EIO
<class 'OSError'> [Errno 5] EIO
<class 'OSError'> [Errno 5] EIO
<class 'OSError'> [Errno 5] EIO
<class 'OSError'> [Errno 5] EIO
<class 'OSError'> [Errno 5] EIO
<class 'TypeError'> can't convert str to int
<class 'TypeError'> can't convert str to int