py/modstruct: Fix struct.unpack with unaligned offset of native type.

With this patch alignment is done relative to the start of the buffer that
is being unpacked, not the raw pointer value, as per CPython.

Fixes issue #3314.
This commit is contained in:
Tom McDermott
2019-08-05 15:15:28 +10:00
committed by Damien George
parent 12f13ee634
commit 1022f9cc35
5 changed files with 24 additions and 6 deletions

View File

@@ -146,6 +146,7 @@ STATIC mp_obj_t struct_unpack_from(size_t n_args, const mp_obj_t *args) {
}
p += offset;
}
byte *p_base = p;
// Check that the input buffer is big enough to unpack all the values
if (p + total_sz > end_p) {
@@ -164,7 +165,7 @@ STATIC mp_obj_t struct_unpack_from(size_t n_args, const mp_obj_t *args) {
res->items[i++] = item;
} else {
while (cnt--) {
item = mp_binary_get_val(fmt_type, *fmt, &p);
item = mp_binary_get_val(fmt_type, *fmt, p_base, &p);
res->items[i++] = item;
}
}