From a14ce77b28146526661c79c89b2e6ff6837c2bb0 Mon Sep 17 00:00:00 2001 From: Bas van Sisseren Date: Sat, 12 Aug 2017 14:40:49 +0200 Subject: [PATCH] py/binary.c: Fix bug when packing big-endian 'Q' values. Without bugfix: struct.pack('>Q', 16) b'\x00\x00\x00\x10\x00\x00\x00\x00' With bugfix: struct.pack('>Q', 16) b'\x00\x00\x00\x00\x00\x00\x00\x10' --- py/binary.c | 5 ++++- tests/basics/struct1_intbig.py | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/py/binary.c b/py/binary.c index e38aae8ea3..870a0942ba 100644 --- a/py/binary.c +++ b/py/binary.c @@ -303,7 +303,10 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte ** // zero/sign extend if needed if (BYTES_PER_WORD < 8 && size > sizeof(val)) { int c = (is_signed(val_type) && (mp_int_t)val < 0) ? 0xff : 0x00; - memset(p + sizeof(val), c, size - sizeof(val)); + memset(p, c, size); + if (struct_type == '>') { + p += size - sizeof(val); + } } } } diff --git a/tests/basics/struct1_intbig.py b/tests/basics/struct1_intbig.py index b1fec527ef..380293f36c 100644 --- a/tests/basics/struct1_intbig.py +++ b/tests/basics/struct1_intbig.py @@ -12,6 +12,8 @@ print(struct.pack("Q", 1)) print(struct.pack("Q", 2**64 - 1)) print(struct.pack("