kopia lustrzana https://github.com/micropython/micropython
py/objtype: Check and prevent delete/store on a fixed locals map.
Note that the check for elem!=NULL is removed for the MP_MAP_LOOKUP_ADD_IF_NOT_FOUND case because mp_map_lookup will always return non-NULL for such a case.pull/3608/head
rodzic
cc92c0572e
commit
b45c8c17f0
12
py/objtype.c
12
py/objtype.c
|
@ -975,21 +975,21 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
||||||
if (self->locals_dict != NULL) {
|
if (self->locals_dict != NULL) {
|
||||||
assert(self->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now
|
assert(self->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now
|
||||||
mp_map_t *locals_map = &self->locals_dict->map;
|
mp_map_t *locals_map = &self->locals_dict->map;
|
||||||
|
if (locals_map->is_fixed) {
|
||||||
|
// can't apply delete/store to a fixed map
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (dest[1] == MP_OBJ_NULL) {
|
if (dest[1] == MP_OBJ_NULL) {
|
||||||
// delete attribute
|
// delete attribute
|
||||||
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
||||||
// note that locals_map may be in ROM, so remove will fail in that case
|
|
||||||
if (elem != NULL) {
|
if (elem != NULL) {
|
||||||
dest[0] = MP_OBJ_NULL; // indicate success
|
dest[0] = MP_OBJ_NULL; // indicate success
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// store attribute
|
// store attribute
|
||||||
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
|
||||||
// note that locals_map may be in ROM, so add will fail in that case
|
elem->value = dest[1];
|
||||||
if (elem != NULL) {
|
dest[0] = MP_OBJ_NULL; // indicate success
|
||||||
elem->value = dest[1];
|
|
||||||
dest[0] = MP_OBJ_NULL; // indicate success
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,3 +30,10 @@ try:
|
||||||
del c.x
|
del c.x
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
print("AttributeError")
|
print("AttributeError")
|
||||||
|
|
||||||
|
# try to del an attribute of a built-in class
|
||||||
|
try:
|
||||||
|
del int.to_bytes
|
||||||
|
except (AttributeError, TypeError):
|
||||||
|
# uPy raises AttributeError, CPython raises TypeError
|
||||||
|
print('AttributeError/TypeError')
|
||||||
|
|
|
@ -16,3 +16,10 @@ try:
|
||||||
setattr(a, b'var3', 1)
|
setattr(a, b'var3', 1)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
print('TypeError')
|
print('TypeError')
|
||||||
|
|
||||||
|
# try setattr on a built-in function
|
||||||
|
try:
|
||||||
|
setattr(int, 'to_bytes', 1)
|
||||||
|
except (AttributeError, TypeError):
|
||||||
|
# uPy raises AttributeError, CPython raises TypeError
|
||||||
|
print('AttributeError/TypeError')
|
||||||
|
|
Ładowanie…
Reference in New Issue