Debian Codegraph @

test/test_seq.py

raw
1 # ruff: noqa: E501
2 # ignore line length limit for long comments
3 import io
4
5 import msgpack
6
7 binarydata = bytes(bytearray(range(256)))
8
9
10 def gen_binary_data(idx):
11 return binarydata[: idx % 300]
12
13
14 def test_exceeding_unpacker_read_size():
15 dumpf = io.BytesIO()
16
17 packer = msgpack.Packer()
18
19 NUMBER_OF_STRINGS = 6
20 read_size = 16
21 # 5 ok for read_size=16, while 6 glibc detected *** python: double free or corruption (fasttop):
22 # 20 ok for read_size=256, while 25 segfaults / glibc detected *** python: double free or corruption (!prev)
23 # 40 ok for read_size=1024, while 50 introduces errors
24 # 7000 ok for read_size=1024*1024, while 8000 leads to glibc detected *** python: double free or corruption (!prev):
25
26 for idx in range(NUMBER_OF_STRINGS):
27 data = gen_binary_data(idx)
28 dumpf.write(packer.pack(data))
29
30 f = io.BytesIO(dumpf.getvalue())
31 dumpf.close()
32
33 unpacker = msgpack.Unpacker(f, read_size=read_size, use_list=1)
34
35 read_count = 0
36 for idx, o in enumerate(unpacker):
37 assert isinstance(o, bytes)
38 assert o == gen_binary_data(idx)
39 read_count += 1
40
41 assert read_count == NUMBER_OF_STRINGS