Don't crash on large messages

This commit is contained in:
Brian Cloutier 2019-09-09 14:12:38 -07:00 committed by Alex Stokes
parent 155bec0562
commit b2146c5268
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086

View File

@ -4,6 +4,8 @@ import math
from libp2p.exceptions import ParseError
from libp2p.io.abc import Reader
from .io.utils import read_exactly
# Unsigned LEB128(varint codec)
# Reference: https://github.com/ethereum/py-wasm/blob/master/wasm/parsers/leb128.py
@ -62,7 +64,7 @@ def encode_varint_prefixed(msg_bytes: bytes) -> bytes:
async def read_varint_prefixed_bytes(reader: Reader) -> bytes:
len_msg = await decode_uvarint_from_stream(reader)
data = await reader.read(len_msg)
data = await read_exactly(reader, len_msg)
if len(data) != len_msg:
raise ValueError(
f"failed to read enough bytes: len_msg={len_msg}, data={data!r}"