Skip to content

Commit

Permalink
extmod/moductypes: Validate that uctypes.struct addr argument is an int.
Browse files Browse the repository at this point in the history
Fixes issue micropython#12660.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Oct 12, 2023
1 parent 5015779 commit 05cb140
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion extmod/moductypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ STATIC NORETURN void syntax_error(void) {
STATIC mp_obj_t uctypes_struct_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 2, 3, false);
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, type);
o->addr = (void *)(uintptr_t)mp_obj_int_get_truncated(args[0]);
o->addr = (void *)(uintptr_t)mp_obj_get_int_truncated(args[0]);
o->desc = args[1];
o->flags = LAYOUT_NATIVE;
if (n_args == 3) {
Expand Down
6 changes: 6 additions & 0 deletions tests/extmod/uctypes_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

data = bytearray(b"01234567")

# first argument isn't an integer
try:
uctypes.struct(data, {})
except TypeError:
print("TypeError")

# del subscr not supported
S = uctypes.struct(uctypes.addressof(data), {})
try:
Expand Down
1 change: 1 addition & 0 deletions tests/extmod/uctypes_error.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ TypeError
TypeError
TypeError
TypeError
TypeError

0 comments on commit 05cb140

Please sign in to comment.