From 5ccd5dc08b596a1e70402b1db4ba737994d9e8a1 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 16 Apr 2021 12:39:23 -0700 Subject: [PATCH 1/3] Fix struct.pack with padding bytes It used to validate the following arg could fit in a single byte. Now, it always uses zero to pad. --- py/binary.c | 6 +----- shared-module/struct/__init__.c | 6 ++++-- tests/basics/struct1.py | 2 ++ 3 files changed, 7 insertions(+), 7 deletions(-) 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/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)) From 390398371ddc96fb803bee47c67b607f555e77ac Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 16 Apr 2021 15:03:29 -0700 Subject: [PATCH 2/3] Update tests/basics/struct1.py Co-authored-by: ajs256 <67526318+ajs256@users.noreply.github.com> From 6620ac8becd5b7addd35c02651b162c0a45a87f2 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 19 Apr 2021 14:58:40 -0700 Subject: [PATCH 3/3] Fix py version of struct too. --- py/modstruct.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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++; } }