From bc46a76029108404c83a28910e9b60208471b8ca Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 25 Sep 2019 16:56:53 -0600 Subject: [PATCH] connection should raise on invalid varint --- libp2p/connection.nim | 7 ++++++- libp2p/muxers/mplex/coder.nim | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/libp2p/connection.nim b/libp2p/connection.nim index b054415c3..bc3d4bce3 100644 --- a/libp2p/connection.nim +++ b/libp2p/connection.nim @@ -23,6 +23,11 @@ type stream*: LPStream observedAddrs*: Multiaddress + InvalidVarintException = object of LPStreamError + +proc newInvalidVarintException*(): ref InvalidVarintException = + result = newException(InvalidVarintException, "unable to prase varint") + proc newConnection*(stream: LPStream): Connection = ## create a new Connection for the specified async reader/writer new result @@ -92,7 +97,7 @@ proc readLp*(s: Connection): Future[seq[byte]] {.async, gcsafe.} = if res == VarintStatus.Success: break if res != VarintStatus.Success or size > DefaultReadSize: - return + raise newInvalidVarintException() result.setLen(size) if size > 0.uint: await s.readExactly(addr result[0], int(size)) diff --git a/libp2p/muxers/mplex/coder.nim b/libp2p/muxers/mplex/coder.nim index d8d4ec2de..2a1055ce0 100644 --- a/libp2p/muxers/mplex/coder.nim +++ b/libp2p/muxers/mplex/coder.nim @@ -19,8 +19,8 @@ logScope: topic = "mplex-coder" type - Msg* = tuple - id: uint + Msg* = tuple + id: uint msgType: MessageType data: seq[byte] @@ -38,7 +38,7 @@ proc readMplexVarint(conn: Connection): Future[Option[uint]] {.async, gcsafe.} = if res == VarintStatus.Success: return some(varint) if res != VarintStatus.Success: - return + raise newInvalidVarintException() except LPStreamIncompleteError: trace "unable to read varint", exc = getCurrentExceptionMsg()