Skip to content
Snippets Groups Projects
Commit a14ce77b authored by Bas van Sisseren's avatar Bas van Sisseren Committed by Damien George
Browse files

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'
parent c127ace2
No related branches found
No related tags found
No related merge requests found
...@@ -303,7 +303,10 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte ** ...@@ -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 // zero/sign extend if needed
if (BYTES_PER_WORD < 8 && size > sizeof(val)) { if (BYTES_PER_WORD < 8 && size > sizeof(val)) {
int c = (is_signed(val_type) && (mp_int_t)val < 0) ? 0xff : 0x00; 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)) ...@@ -12,6 +12,8 @@ print(struct.pack("<I", 2**32 - 1))
print(struct.pack("<I", 0xffffffff)) print(struct.pack("<I", 0xffffffff))
# long long ints # 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", 2**64 - 1)) print(struct.pack(">Q", 2**64 - 1))
print(struct.pack("<Q", 0xffffffffffffffff)) print(struct.pack("<Q", 0xffffffffffffffff))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment