Merge pull request #4620 from tannewt/fix_struct_pack

Fix struct.pack with padding bytes
This commit is contained in:
Jeff Epler 2021-04-19 20:43:20 -05:00 committed by GitHub
commit 18548fc5d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 9 deletions

View File

@ -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) {

View File

@ -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++;
}
}

View File

@ -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++;
}
}

View File

@ -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))