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'
crypto-aes
Bas van Sisseren 6 years ago committed by Damien George
parent c127ace28a
commit a14ce77b28

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

@ -12,6 +12,8 @@ print(struct.pack("<I", 2**32 - 1))
print(struct.pack("<I", 0xffffffff))
# long long ints
print(struct.pack("<Q", 1))
print(struct.pack(">Q", 1))
print(struct.pack("<Q", 2**64 - 1))
print(struct.pack(">Q", 2**64 - 1))
print(struct.pack("<Q", 0xffffffffffffffff))

Loading…
Cancel
Save