Browse Source

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/3264/merge
Bas van Sisseren 7 years ago
committed by Damien George
parent
commit
a14ce77b28
  1. 5
      py/binary.c
  2. 2
      tests/basics/struct1_intbig.py

5
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 // 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);
}
} }
} }
} }

2
tests/basics/struct1_intbig.py

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

Loading…
Cancel
Save