mirror of https://github.com/vacp2p/nim-libp2p.git
connection should raise on invalid varint
This commit is contained in:
parent
15bae7bec5
commit
bc46a76029
|
@ -23,6 +23,11 @@ type
|
||||||
stream*: LPStream
|
stream*: LPStream
|
||||||
observedAddrs*: Multiaddress
|
observedAddrs*: Multiaddress
|
||||||
|
|
||||||
|
InvalidVarintException = object of LPStreamError
|
||||||
|
|
||||||
|
proc newInvalidVarintException*(): ref InvalidVarintException =
|
||||||
|
result = newException(InvalidVarintException, "unable to prase varint")
|
||||||
|
|
||||||
proc newConnection*(stream: LPStream): Connection =
|
proc newConnection*(stream: LPStream): Connection =
|
||||||
## create a new Connection for the specified async reader/writer
|
## create a new Connection for the specified async reader/writer
|
||||||
new result
|
new result
|
||||||
|
@ -92,7 +97,7 @@ proc readLp*(s: Connection): Future[seq[byte]] {.async, gcsafe.} =
|
||||||
if res == VarintStatus.Success:
|
if res == VarintStatus.Success:
|
||||||
break
|
break
|
||||||
if res != VarintStatus.Success or size > DefaultReadSize:
|
if res != VarintStatus.Success or size > DefaultReadSize:
|
||||||
return
|
raise newInvalidVarintException()
|
||||||
result.setLen(size)
|
result.setLen(size)
|
||||||
if size > 0.uint:
|
if size > 0.uint:
|
||||||
await s.readExactly(addr result[0], int(size))
|
await s.readExactly(addr result[0], int(size))
|
||||||
|
|
|
@ -19,8 +19,8 @@ logScope:
|
||||||
topic = "mplex-coder"
|
topic = "mplex-coder"
|
||||||
|
|
||||||
type
|
type
|
||||||
Msg* = tuple
|
Msg* = tuple
|
||||||
id: uint
|
id: uint
|
||||||
msgType: MessageType
|
msgType: MessageType
|
||||||
data: seq[byte]
|
data: seq[byte]
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ proc readMplexVarint(conn: Connection): Future[Option[uint]] {.async, gcsafe.} =
|
||||||
if res == VarintStatus.Success:
|
if res == VarintStatus.Success:
|
||||||
return some(varint)
|
return some(varint)
|
||||||
if res != VarintStatus.Success:
|
if res != VarintStatus.Success:
|
||||||
return
|
raise newInvalidVarintException()
|
||||||
except LPStreamIncompleteError:
|
except LPStreamIncompleteError:
|
||||||
trace "unable to read varint", exc = getCurrentExceptionMsg()
|
trace "unable to read varint", exc = getCurrentExceptionMsg()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue