diff --git a/py/binary.c b/py/binary.c index 846058525f..cd61d74fa8 100644 --- a/py/binary.c +++ b/py/binary.c @@ -371,11 +371,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte ** } } - if (val_type == 'x') { - memset(p, 0, 1); - } else { - mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val); - } + mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val); } void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) { diff --git a/py/modstruct.c b/py/modstruct.c index 1c5319608d..8f78fa234a 100644 --- a/py/modstruct.c +++ b/py/modstruct.c @@ -214,9 +214,11 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c p += cnt; } else { while (cnt--) { - mp_binary_set_val(fmt_type, *fmt, args[i], &p); // Pad bytes don't have a corresponding argument. - if (*fmt != 'x') { + if (*fmt == 'x') { + mp_binary_set_val(fmt_type, *fmt, MP_OBJ_NEW_SMALL_INT(0), &p); + } else { + mp_binary_set_val(fmt_type, *fmt, args[i], &p); i++; } } diff --git a/shared-module/struct/__init__.c b/shared-module/struct/__init__.c index 7a7ea845e7..258036c93b 100644 --- a/shared-module/struct/__init__.c +++ b/shared-module/struct/__init__.c @@ -153,9 +153,11 @@ void shared_modules_struct_pack_into(mp_obj_t fmt_in, byte *p, byte *end_p, size p += sz; } else { while (sz--) { - mp_binary_set_val(fmt_type, *fmt, args[i], &p); // Pad bytes don't have a corresponding argument. - if (*fmt != 'x') { + if (*fmt == 'x') { + mp_binary_set_val(fmt_type, *fmt, MP_OBJ_NEW_SMALL_INT(0), &p); + } else { + mp_binary_set_val(fmt_type, *fmt, args[i], &p); i++; } } diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py index 107006cc3f..ec29ea9080 100644 --- a/tests/basics/struct1.py +++ b/tests/basics/struct1.py @@ -121,3 +121,5 @@ except: # check padding bytes print(struct.pack("xb", 3)) +# Make sure pack doesn't reuse a larger value and error +print(struct.pack("xH", 0x100))