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'
pull/1/head
Bas van Sisseren 2017-08-12 14:40:49 +02:00 committed by Damien George
parent c127ace28a
commit a14ce77b28
2 changed files with 6 additions and 1 deletions

View File

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

View File

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