net: make Dial, Listen{,Packet} for TCP/UDP with invalid port fail

This change makes Dial, Listen and ListenPacket with invalid port fail
whatever GODEBUG=netdns is.

Please be informed that cgoLookupPort with an out of range literal
number may return either the lower or upper bound value, 0 or 65535,
with no error on some platform.

Fixes #11715.

Change-Id: I43f9c4fb5526d1bf50b97698e0eb39d29fd74c35
Reviewed-on: https://go-review.googlesource.com/12447
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Mikio Hara 2015-07-21 12:51:01 +09:00
parent 3d5163cf43
commit b50b21d3e1
7 changed files with 93 additions and 38 deletions

View file

@ -118,15 +118,27 @@ const big = 0xFFFFFF
// Returns number, new offset, success.
func dtoi(s string, i0 int) (n int, i int, ok bool) {
n = 0
neg := false
if len(s) > 0 && s[0] == '-' {
neg = true
s = s[1:]
}
for i = i0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
n = n*10 + int(s[i]-'0')
if n >= big {
return 0, i, false
if neg {
return -big, i + 1, false
}
return big, i, false
}
}
if i == i0 {
return 0, i, false
}
if neg {
n = -n
i++
}
return n, i, true
}