summaryrefslogtreecommitdiffstatshomepage
path: root/py/objexcept.c
diff options
context:
space:
mode:
authorYonatan Goldschmidt <yon.goldschmidt@gmail.com>2020-01-22 13:34:19 +0100
committerDamien George <damien@micropython.org>2022-07-18 11:17:46 +1000
commit2a6ba47110be88ff1e1f5abd1bd76c353447884c (patch)
treebfcbdca7337b6e47d1315dbf14cb339833d0e944 /py/objexcept.c
parent6670281472aa3c931b08e623488a02c335aeda20 (diff)
downloadmicropython-2a6ba47110be88ff1e1f5abd1bd76c353447884c.tar.gz
micropython-2a6ba47110be88ff1e1f5abd1bd76c353447884c.zip
py/obj: Add static safety checks to mp_obj_is_type().
Commit d96cfd13e3a464862c introduced a regression by breaking existing users of mp_obj_is_type(.., &mp_obj_bool). This function (and associated helpers like mp_obj_is_int()) have some specific nuances, and mistakes like this one can happen again. This commit adds mp_obj_is_exact_type() which behaves like the the old mp_obj_is_type(). The new mp_obj_is_type() has the same prototype but it attempts to statically assert that it's not called with types which should be checked using mp_obj_is_type(). If called with any of these types: int, str, bool, NoneType - it will cause a compilation error. Additional checked types (e.g function types) can be added in the future. Existing users of mp_obj_is_type() with the now "invalid" types, were translated to use mp_obj_is_exact_type(). The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other compilers) can't statically check conditions that are only known during link-time (like variables' addresses comparison). However, in this case, GCC is able to statically detect these conditions, probably because it's the exact same object - `&mp_type_int == &mp_type_int` is detected. Misuses of this function with runtime-chosen types (e.g: `mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected. MSC is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC(). Compiling with this commit and without the fix for d96cfd13e3a464862c shows that it detects the problem. Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
Diffstat (limited to 'py/objexcept.c')
-rw-r--r--py/objexcept.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/py/objexcept.c b/py/objexcept.c
index dca287bb6e..028b73fd8b 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -122,7 +122,7 @@ STATIC mp_obj_exception_t *get_native_exception(mp_obj_t self_in) {
STATIC void decompress_error_text_maybe(mp_obj_exception_t *o) {
#if MICROPY_ROM_TEXT_COMPRESSION
- if (o->args->len == 1 && mp_obj_is_type(o->args->items[0], &mp_type_str)) {
+ if (o->args->len == 1 && mp_obj_is_exact_type(o->args->items[0], &mp_type_str)) {
mp_obj_str_t *o_str = MP_OBJ_TO_PTR(o->args->items[0]);
if (MP_IS_COMPRESSED_ROM_STRING(o_str->data)) {
byte *buf = m_new_maybe(byte, MP_MAX_UNCOMPRESSED_TEXT_LEN + 1);