1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // parsePort parses service as a decimal integer and returns the
8 // corresponding value as port. It is the caller's responsibility to
9 // parse service as a non-decimal integer when needsLookup is true.
11 // Some system resolvers will return a valid port number when given a number
12 // over 65536 (see https://golang.org/issues/11715). Alas, the parser
13 // can't bail early on numbers > 65536. Therefore reasonably large/small
14 // numbers are parsed in full and rejected if invalid.
15 func parsePort(service
string) (port
int, needsLookup
bool) {
17 // Lock in the legacy behavior that an empty string
18 // means port 0. See golang.org/issue/13610.
22 max
= uint32(1<<32 - 1)
23 cutoff
= uint32(1 << 30)
26 if service
[0] == '+' {
28 } else if service
[0] == '-' {
33 for _
, d
:= range service
{
34 if '0' <= d
&& d
<= '9' {
45 if nn
< n || nn
> max
{
51 if !neg
&& n
>= cutoff
{
52 port
= int(cutoff
- 1)
53 } else if neg
&& n
> cutoff
{