8000 extmod/vfs_blockdev: Check block device function positive results. · projectgus/micropython@a26ccdd · GitHub
[go: up one dir, main page]

Skip to content

Commit a26ccdd

Browse files
committed
extmod/vfs_blockdev: Check block device function positive results.
A positive result here can result in eventual memory corruption as littlefs expects the result of a cache read/write function to be 0 or a negative integer for an error. Closes micropython#13046 This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 457f2cc commit a26ccdd

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

extmod/vfs_blockdev.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232

3333
#if MICROPY_VFS
3434

35+
// Block device functions are expected to return 0 on success
36+
// and negative integer on errors. Check for positive integer
37+
// results as some callers (i.e. littlefs) will produce corrupt
38+
// results from these.
39+
STATIC int mp_vfs_check_result(mp_obj_t ret) {
40+
int i = MP_OBJ_SMALL_INT_VALUE(ret);
41+
if (i > 0) {
42+
mp_raise_OSError(MP_EINVAL);
43+
}
44+
return i;
45+
}
46+
3547
void mp_vfs_blockdev_init(mp_vfs_blockdev_t *self, mp_obj_t bdev) {
3648
mp_load_method(bdev, MP_QSTR_readblocks, self->readblocks);
3749
mp_load_method_maybe(bdev, MP_QSTR_writeblocks, self->writeblocks);
@@ -69,7 +81,7 @@ int mp_vfs_blockdev_read_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t b
6981
if (ret == mp_const_none) {
7082
return 0;
7183
} else {
72-
return MP_OBJ_SMALL_INT_VALUE(ret);
84+
return mp_vfs_check_result(ret);
7385
}
7486
}
7587

@@ -106,7 +118,7 @@ int mp_vfs_blockdev_write_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t
106118
if (ret == mp_const_none) {
107119
return 0;
108120
} else {
109-
return MP_OBJ_SMALL_INT_VALUE(ret);
121+
return mp_vfs_check_result(ret);
110122
}
111123
}
112124

0 commit comments

Comments
 (0)
0