Fixed overflow in sockets.parseIP4.

This commit is contained in:
Dominik Picheta
2013-05-24 19:43:37 +01:00
parent 4e3ac4319b
commit 3001ed54f9

View File

@@ -369,8 +369,11 @@ proc listen*(socket: TSocket, backlog = SOMAXCONN) {.tags: [FReadIO].} =
proc invalidIp4(s: string) {.noreturn, noinline.} =
raise newException(EInvalidValue, "invalid ip4 address: " & s)
proc parseIp4*(s: string): int32 =
proc parseIp4*(s: string): biggestInt =
## parses an IP version 4 in dotted decimal form like "a.b.c.d".
##
## This is equivalent to `inet_ntoa`:idx:.
##
## Raises EInvalidValue in case of an error.
var a, b, c, d: int
var i = 0
@@ -393,7 +396,7 @@ proc parseIp4*(s: string): int32 =
if j <= 0: invalidIp4(s)
inc(i, j)
if s[i] != '\0': invalidIp4(s)
result = int32(a shl 24 or b shl 16 or c shl 8 or d)
result = biggestInt(a shl 24 or b shl 16 or c shl 8 or d)
template gaiNim(a, p, h, list: expr): stmt =
block: