@@ -58,7 +58,7 @@ func parsePortSpecs(ports []string) (exposedPorts []string, _ error) {
58
58
func splitProtoPort (rawPort string ) (proto string , port string , _ error ) {
59
59
port , proto , _ = strings .Cut (rawPort , "/" )
60
60
if port == "" {
61
- return "" , "" , errors .Errorf ("no port specified: %s<empty>" , rawPort )
61
+ return "" , "" , errors .New ("no port specified" )
62
62
}
63
63
proto = strings .ToLower (proto )
64
64
switch proto {
@@ -92,7 +92,7 @@ func parsePortSpec(rawPort string) (portProto []string, _ error) {
92
92
ip , hostPort , containerPort := splitParts (rawPort )
93
93
proto , containerPort , err := splitProtoPort (containerPort )
94
94
if err != nil {
95
- return nil , err
95
+ return nil , errors . Wrapf ( err , "invalid port: %q" , rawPort )
96
96
}
97
97
98
98
// TODO(thaJeztah): mapping IP-addresses should not be allowed for EXPOSE; see https://github.com/moby/buildkit/issues/2173
@@ -114,9 +114,8 @@ func parsePortSpec(rawPort string) (portProto []string, _ error) {
114
114
}
115
115
116
116
// TODO(thaJeztah): mapping host-ports should not be allowed for EXPOSE; see https://github.com/moby/buildkit/issues/2173
117
- var startHostPort , endHostPort uint64
118
117
if hostPort != "" {
119
- startHostPort , endHostPort , err = parsePortRange (hostPort )
118
+ startHostPort , endHostPort , err : = parsePortRange (hostPort )
120
119
if err != nil {
121
120
return nil , errors .New ("invalid hostPort: " + hostPort )
122
121
}
@@ -134,36 +133,53 @@ func parsePortSpec(rawPort string) (portProto []string, _ error) {
134
133
ports := make ([]string , 0 , count )
135
134
136
135
for i := range count {
137
- ports = append (ports , strconv .FormatUint (startPort + i , 10 )+ "/" + proto )
136
+ ports = append (ports , strconv .Itoa (startPort + i )+ "/" + proto )
138
137
}
139
138
return ports , nil
140
139
}
141
140
142
- // parsePortRange parses and validates the specified string as a port- range (8000-9000)
143
- func parsePortRange (ports string ) (uint64 , uint64 , error ) {
141
+ // parsePortRange parses and validates the specified string as a port range (e.g., " 8000-9000").
142
+ func parsePortRange (ports string ) (startPort , endPort int , _ error ) {
144
143
if ports == "" {
145
144
return 0 , 0 , errors .New ("empty string specified for ports" )
146
145
}
147
- if ! strings .Contains (ports , "-" ) {
148
- start , err := strconv .ParseUint (ports , 10 , 16 )
149
- end := start
150
- return start , end , err
151
- }
146
+ start , end , ok := strings .Cut (ports , "-" )
152
147
153
- parts := strings .Split (ports , "-" )
154
- if len (parts ) != 2 {
155
- return 0 , 0 , errors .Errorf ("invalid port range format: %s" , ports )
148
+ startPort , err := parsePortNumber (start )
149
+ if err != nil {
150
+ return 0 , 0 , fmt .Errorf ("invalid start port '%s': %w" , start , err )
151
+ }
152
+ if ! ok || start == end {
153
+ return startPort , startPort , nil
156
154
}
157
- start , err := strconv .ParseUint (parts [0 ], 10 , 16 )
155
+
156
+ endPort , err = parsePortNumber (end )
158
157
if err != nil {
159
- return 0 , 0 , err
158
+ return 0 , 0 , fmt .Errorf ("invalid end port '%s': %w" , end , err )
159
+ }
160
+ if endPort < startPort {
161
+ return 0 , 0 , errors .New ("invalid port range: " + ports )
160
162
}
161
- end , err := strconv .ParseUint (parts [1 ], 10 , 16 )
163
+ return startPort , endPort , nil
164
+ }
165
+
166
+ // parsePortNumber parses rawPort into an int, unwrapping strconv errors
167
+ // and returning a single "out of range" error for any value outside 0–65535.
168
+ func parsePortNumber (rawPort string ) (int , error ) {
169
+ if rawPort == "" {
170
+ return 0 , errors .New ("value is empty" )
171
+ }
172
+ port , err := strconv .ParseInt (rawPort , 10 , 0 )
162
173
if err != nil {
163
- return 0 , 0 , err
174
+ var numErr * strconv.NumError
175
+ if errors .As (err , & numErr ) {
176
+ err = numErr .Err
177
+ }
178
+ return 0 , err
164
179
}
165
- if end < start {
166
- return 0 , 0 , errors .New ("invalid range specified for port: " + ports )
180
+ if port < 0 || port > 65535 {
181
+ return 0 , errors .New ("value out of range (0–65535)" )
167
182
}
168
- return start , end , nil
183
+
184
+ return int (port ), nil
169
185
}
0 commit comments