From 66f0afc91d22bb414d8247b795ce37743bd3f39d Mon Sep 17 00:00:00 2001 From: Yonatan Goldschmidt Date: Sun, 10 Feb 2019 00:36:08 +0200 Subject: [PATCH] unix/modmachine: Handle repeated /dev/mem open errors. If opening of /dev/mem has failed an `OSError` is appropriately raised, but the next time `mem8/16/32` is accessed the invalid file descriptor is used and the program gets a SIGSEGV. --- ports/unix/modmachine.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/unix/modmachine.c b/ports/unix/modmachine.c index 48dddec0a..e2c44f94c 100644 --- a/ports/unix/modmachine.c +++ b/ports/unix/modmachine.c @@ -57,10 +57,11 @@ uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) { static uintptr_t last_base = (uintptr_t)-1; static uintptr_t map_page; if (!fd) { - fd = open("/dev/mem", O_RDWR | O_SYNC); - if (fd == -1) { + int _fd = open("/dev/mem", O_RDWR | O_SYNC); + if (_fd == -1) { mp_raise_OSError(errno); } + fd = _fd; } uintptr_t cur_base = addr & ~MICROPY_PAGE_MASK;