fix: correct size of uint32 with cython (#415)

* chore: add more coverage for big endian systems

* chore: add more coverage for big endian systems

* chore: fix overflow

* fix: should have been unsigned int
This commit is contained in:
J. Nick Koston 2025-03-06 14:13:00 -10:00 committed by GitHub
parent b934fbd5d0
commit f64eb58573
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 1 deletions

View File

@ -117,7 +117,7 @@ cdef cython.uint EWOULDBLOCK
cdef get_signature_tree
cdef unsigned long _ustr_uint32(const unsigned char * buf, unsigned int offset, unsigned int endian) noexcept
cdef unsigned int _ustr_uint32(const unsigned char * buf, unsigned int offset, unsigned int endian) noexcept
cdef short _ustr_int16(const unsigned char * buf, unsigned int offset, unsigned int endian) noexcept

View File

@ -21,38 +21,100 @@ from dbus_fast.unpack import unpack_variants
def test_bytearray_to_uint32_big_end():
assert buffer_to_uint32(bytearray(b"\x01\x02\x03\x04"), 0, BIG_ENDIAN) == 16909060
assert (
buffer_to_uint32(
bytearray((0xFFFFFFFF).to_bytes(4, byteorder="big", signed=False)),
0,
BIG_ENDIAN,
)
== 0xFFFFFFFF
)
def test_bytearray_to_uint16_big_end():
assert buffer_to_uint16(bytearray(b"\x01\x02"), 0, BIG_ENDIAN) == 258
assert (
buffer_to_uint16(
bytearray((0xFFFF).to_bytes(2, byteorder="big", signed=False)),
0,
BIG_ENDIAN,
)
== 0xFFFF
)
def test_bytearray_to_int16_big_end():
assert buffer_to_int16(bytearray(b"\x01\x02"), 0, BIG_ENDIAN) == 258
assert (
buffer_to_int16(
bytearray((32767).to_bytes(2, byteorder="big", signed=True)), 0, BIG_ENDIAN
)
== 32767
)
@pytest.mark.skipif(not is_compiled(), reason="requires cython")
def test_bytearray_to_int16_big_end_signed():
assert buffer_to_int16(bytearray(b"\xff\xff"), 0, BIG_ENDIAN) == -1
assert (
buffer_to_int16(
bytearray((-32768).to_bytes(2, byteorder="big", signed=True)),
0,
BIG_ENDIAN,
)
== -32768
)
def test_bytearray_to_uint32_little_end():
assert (
buffer_to_uint32(bytearray(b"\x01\x02\x03\x04"), 0, LITTLE_ENDIAN) == 67305985
)
assert (
buffer_to_uint32(
bytearray((0xFFFFFFFF).to_bytes(4, byteorder="little", signed=False)),
0,
LITTLE_ENDIAN,
)
== 0xFFFFFFFF
)
def test_bytearray_to_uint16_little_end():
assert buffer_to_uint16(bytearray(b"\x01\x02"), 0, LITTLE_ENDIAN) == 513
assert (
buffer_to_uint16(
bytearray((0xFFFF).to_bytes(2, byteorder="little", signed=False)),
0,
LITTLE_ENDIAN,
)
== 0xFFFF
)
def test_bytearray_to_int16_little_end():
assert buffer_to_int16(bytearray(b"\x01\x02"), 0, LITTLE_ENDIAN) == 513
assert (
buffer_to_int16(
bytearray((32767).to_bytes(2, byteorder="little", signed=True)),
0,
LITTLE_ENDIAN,
)
== 32767
)
@pytest.mark.skipif(not is_compiled(), reason="requires cython")
def test_bytearray_to_int16_little_end_signed():
assert buffer_to_int16(bytearray(b"\xff\xff"), 0, LITTLE_ENDIAN) == -1
assert (
buffer_to_int16(
bytearray((-32768).to_bytes(2, byteorder="little", signed=True)),
0,
LITTLE_ENDIAN,
)
== -32768
)
def print_buf(buf):