From 51c87bf090b48ab40be05ea009aae9b4034d1dae Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 27 Mar 2019 21:42:46 +0200 Subject: [PATCH] Fix to allow parsing of addresses with 0 port. --- chronos/transports/common.nim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/chronos/transports/common.nim b/chronos/transports/common.nim index e55ac50..2e0fda7 100644 --- a/chronos/transports/common.nim +++ b/chronos/transports/common.nim @@ -200,19 +200,17 @@ proc initTAddress*(address: string): TransportAddress = else: (sizeof(result.address_un) - 1) copyMem(addr result.address_un[0], unsafeAddr address[0], size) else: - var port: Port + var port: int var parts = address.rsplit(":", maxsplit = 1) if len(parts) != 2: raise newException(TransportAddressError, "Format is
: or
!") - try: - let portint = parseInt(parts[1]) - doAssert(portint > 0 and portint < 65536) - port = Port(portint) + port = parseInt(parts[1]) except: raise newException(TransportAddressError, "Illegal port number!") - + if port < 0 or port >= 65536: + raise newException(TransportAddressError, "Illegal port number!") try: var ipaddr: IpAddress if parts[0][0] == '[' and parts[0][^1] == ']': @@ -227,7 +225,7 @@ proc initTAddress*(address: string): TransportAddress = result.address_v6 = ipaddr.address_v6 else: raise newException(TransportAddressError, "Incorrect address family!") - result.port = port + result.port = Port(port) except: raise newException(TransportAddressError, getCurrentException().msg) else: @@ -388,10 +386,12 @@ proc resolveTAddress*(address: string, try: port = parseInt(parts[1]) - doAssert(port > 0 and port < 65536) except: raise newException(TransportAddressError, "Illegal port number!") + if port < 0 or port >= 65536: + raise newException(TransportAddressError, "Illegal port number!") + if parts[0][0] == '[' and parts[0][^1] == ']': # IPv6 numeric addresses must be enclosed with `[]`. hostname = parts[0][1..^2]