2011-01-09 23:29:18 +09:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2012-06-19 14:20:56 +09:00
|
|
|
import io
|
2011-01-09 23:29:18 +09:00
|
|
|
import msgpack
|
|
|
|
|
2014-02-15 22:20:57 +09:00
|
|
|
|
|
|
|
binarydata = bytes(bytearray(range(256)))
|
2011-01-09 23:29:18 +09:00
|
|
|
|
2019-12-05 18:51:45 +09:00
|
|
|
|
2011-01-09 23:29:18 +09:00
|
|
|
def gen_binary_data(idx):
|
2019-12-05 18:51:45 +09:00
|
|
|
return binarydata[: idx % 300]
|
2014-02-15 22:20:57 +09:00
|
|
|
|
2011-01-09 23:29:18 +09:00
|
|
|
|
|
|
|
def test_exceeding_unpacker_read_size():
|
2012-06-19 14:20:56 +09:00
|
|
|
dumpf = io.BytesIO()
|
2011-01-09 23:29:18 +09:00
|
|
|
|
|
|
|
packer = msgpack.Packer()
|
|
|
|
|
|
|
|
NUMBER_OF_STRINGS = 6
|
|
|
|
read_size = 16
|
2019-12-05 18:51:45 +09:00
|
|
|
# 5 ok for read_size=16, while 6 glibc detected *** python: double free or corruption (fasttop):
|
|
|
|
# 20 ok for read_size=256, while 25 segfaults / glibc detected *** python: double free or corruption (!prev)
|
|
|
|
# 40 ok for read_size=1024, while 50 introduces errors
|
|
|
|
# 7000 ok for read_size=1024*1024, while 8000 leads to glibc detected *** python: double free or corruption (!prev):
|
2011-01-09 23:29:18 +09:00
|
|
|
|
2012-06-19 14:20:56 +09:00
|
|
|
for idx in range(NUMBER_OF_STRINGS):
|
2011-01-09 23:29:18 +09:00
|
|
|
data = gen_binary_data(idx)
|
|
|
|
dumpf.write(packer.pack(data))
|
|
|
|
|
2012-06-19 14:20:56 +09:00
|
|
|
f = io.BytesIO(dumpf.getvalue())
|
2011-01-09 23:29:18 +09:00
|
|
|
dumpf.close()
|
|
|
|
|
2012-09-24 02:12:55 +09:00
|
|
|
unpacker = msgpack.Unpacker(f, read_size=read_size, use_list=1)
|
2011-01-09 23:29:18 +09:00
|
|
|
|
|
|
|
read_count = 0
|
|
|
|
for idx, o in enumerate(unpacker):
|
2023-09-05 03:51:04 +02:00
|
|
|
assert isinstance(o, bytes)
|
2012-12-29 11:24:25 +09:00
|
|
|
assert o == gen_binary_data(idx)
|
2011-01-09 23:29:18 +09:00
|
|
|
read_count += 1
|
|
|
|
|
2012-12-29 11:24:25 +09:00
|
|
|
assert read_count == NUMBER_OF_STRINGS
|