Skip to content

Commit 30acb16

Browse files
committed
extmod/vfs_rom: Remove ability to create VfsRom from an address.
It's not necessary to support this, which allows an arbitrary memory address to be specified and potentially allows invalid memory accesses. Requiring an object with the buffer protocol is safer, and also means that the length of the region is always specified. Signed-off-by: Damien George <[email protected]>
1 parent e40a3fd commit 30acb16

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

extmod/vfs_rom.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,13 @@ static mp_obj_t vfs_rom_make_new(const mp_obj_type_t *type, size_t n_args, size_
198198
self->base.type = type;
199199
self->memory = args[0];
200200

201+
// Get the ROMFS memory region.
201202
mp_buffer_info_t bufinfo;
202-
if (mp_get_buffer(self->memory, &bufinfo, MP_BUFFER_READ)) {
203-
if (bufinfo.len < ROMFS_SIZE_MIN) {
204-
mp_raise_OSError(MP_ENODEV);
205-
}
206-
self->filesystem = bufinfo.buf;
207-
} else {
208-
self->filesystem = (const uint8_t *)(uintptr_t)mp_obj_get_int_truncated(self->memory);
203+
mp_get_buffer_raise(self->memory, &bufinfo, MP_BUFFER_READ);
204+
if (bufinfo.len < ROMFS_SIZE_MIN) {
205+
mp_raise_OSError(MP_ENODEV);
209206
}
207+
self->filesystem = bufinfo.buf;
210208

211209
// Verify it is a ROMFS.
212210
if (!(self->filesystem[0] == ROMFS_HEADER_BYTE0

tests/extmod/vfs_rom.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ def test_unknown_record(self):
226226
class TestStandalone(TestBase):
227227
def test_constructor(self):
228228
self.assertIsInstance(vfs.VfsRom(self.romfs), vfs.VfsRom)
229-
self.assertIsInstance(vfs.VfsRom(self.romfs_addr), vfs.VfsRom)
229+
with self.assertRaises(TypeError):
230+
vfs.VfsRom(self.romfs_addr)
230231

231232
def test_mount(self):
232233
vfs.VfsRom(self.romfs).mount(True, False)

0 commit comments

Comments
 (0)