diff options
author | Simon Arlott <sa.me.uk> | 2022-11-01 18:44:23 +0000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2022-11-08 23:03:21 +1100 |
commit | f8b0ae32d31cfb95db4294c43b821c205c17e1d3 (patch) | |
tree | 4b374053ff540b92c5410da88f4da180944908a7 /py | |
parent | 43dd3ea74dad79dc144d03a1caba8a4f220a5b27 (diff) |
py/builtinimport: Fix crash handling "weak link" module names.
There are two calls to mp_builtin___import__():
1. ports/unix/main.c:main_() which provides a str in args[0]
2. py/runtime.c:mp_import_name() which provides a qstr in args[0]
The default implementation of mp_builtin___import__() is
mp_builtin___import___default() which has a different implementation based
on MICROPY_ENABLE_EXTERNAL_IMPORT.
If MICROPY_ENABLE_EXTERNAL_IMPORT is disabled then the handling of weak
links assumes that args[0] is a `const char *`, when it is either a str or
qstr object.
Use the existing qstr of the module name instead, and also use a vstr
instead of strcpy() to ensure no overflow occurs.
Diffstat (limited to 'py')
-rw-r--r-- | py/builtinimport.c | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index a578d4ad2..aa31b69db 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -45,6 +45,15 @@ #define DEBUG_printf(...) (void)0 #endif +#if MICROPY_MODULE_WEAK_LINKS +STATIC qstr make_weak_link_name(vstr_t *buffer, qstr name) { + vstr_reset(buffer); + vstr_add_char(buffer, 'u'); + vstr_add_str(buffer, qstr_str(name)); + return qstr_from_strn(buffer->buf, buffer->len); +} +#endif + #if MICROPY_ENABLE_EXTERNAL_IMPORT // Must be a string of one byte. @@ -367,10 +376,7 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name, // formerly known as "weak links"). #if MICROPY_MODULE_WEAK_LINKS if (stat == MP_IMPORT_STAT_NO_EXIST && module_obj == MP_OBJ_NULL) { - char *umodule_buf = vstr_str(path); - umodule_buf[0] = 'u'; - strcpy(umodule_buf + 1, qstr_str(level_mod_name)); - qstr umodule_name = qstr_from_str(umodule_buf); + qstr umodule_name = make_weak_link_name(path, level_mod_name); module_obj = mp_module_get_builtin(umodule_name); } #elif MICROPY_PY_SYS @@ -581,10 +587,8 @@ mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) { #if MICROPY_MODULE_WEAK_LINKS // Check if there is a weak link to this module - char umodule_buf[MICROPY_ALLOC_PATH_MAX]; - umodule_buf[0] = 'u'; - strcpy(umodule_buf + 1, args[0]); - qstr umodule_name_qstr = qstr_from_str(umodule_buf); + VSTR_FIXED(umodule_path, MICROPY_ALLOC_PATH_MAX); + qstr umodule_name_qstr = make_weak_link_name(&umodule_path, module_name_qstr); module_obj = mp_module_get_loaded_or_builtin(umodule_name_qstr); if (module_obj != MP_OBJ_NULL) { return module_obj; |