summaryrefslogtreecommitdiff
path: root/py/binary.c
diff options
context:
space:
mode:
authorTom McDermott <spon@wattwatchers.com.au>2019-08-05 15:15:28 +1000
committerDamien George <damien.p.george@gmail.com>2019-09-02 13:10:55 +1000
commit1022f9cc35564b216a4bcd7c65e8243c810a0ca9 (patch)
treebc2d7d12d447a63f6a4c1c9e0f862a616a9b9a80 /py/binary.c
parent12f13ee6346d8fd029fc2ecec06d50b5f7f6b252 (diff)
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.
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/py/binary.c b/py/binary.c
index a142776c3..9810e4660 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -185,14 +185,14 @@ long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, con
}
#define is_signed(typecode) (typecode > 'Z')
-mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
+mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr) {
byte *p = *ptr;
mp_uint_t align;
size_t size = mp_binary_get_size(struct_type, val_type, &align);
if (struct_type == '@') {
- // Make pointer aligned
- p = (byte*)MP_ALIGN(p, (size_t)align);
+ // Align p relative to p_base
+ p = p_base + (uintptr_t)MP_ALIGN(p - p_base, (size_t)align);
#if MP_ENDIANNESS_LITTLE
struct_type = '<';
#else