Skip to content

Commit 81b2f3e

Browse files
committed
Fix #80114: parse_url does not accept URLs with port 0
URIs with a 0 port are generally valid, so `parse_url()` should recognize such URIs, but still report the port as missing. Co-authored-by: twosee <twose@qq.com> Closes GH-6152.
1 parent a754441 commit 81b2f3e

13 files changed

+36
-4
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ PHP NEWS
1212
. Fixed bug #80083 (Optimizer pass 6 removes variables used for ibm_db2 data
1313
binding). (Nikita)
1414

15+
- Standard:
16+
. Fixed bug #80114 (parse_url does not accept URLs with port 0). (cmb, twosee)
17+
1518
01 Oct 2020, PHP 7.3.23
1619

1720
- Core:

ext/standard/tests/url/parse_url_basic_001.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,15 @@ echo "Done";
859859
string(3) "%:x"
860860
}
861861

862+
--> https://example.com:0/: array(3) {
863+
["scheme"]=>
864+
string(5) "https"
865+
["host"]=>
866+
string(11) "example.com"
867+
["path"]=>
868+
string(1) "/"
869+
}
870+
862871
--> http:///blah.com: bool(false)
863872

864873
--> http://:80: bool(false)

ext/standard/tests/url/parse_url_basic_002.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ echo "Done";
113113
--> / : NULL
114114
--> /rest/Users?filter={"id":"123"} : NULL
115115
--> %:x : NULL
116+
--> https://example.com:0/ : string(5) "https"
116117
--> http:///blah.com : bool(false)
117118
--> http://:80 : bool(false)
118119
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_basic_003.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ echo "Done";
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114114
--> %:x : NULL
115+
--> https://example.com:0/ : string(11) "example.com"
115116
--> http:///blah.com : bool(false)
116117
--> http://:80 : bool(false)
117118
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_basic_004.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ echo "Done";
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114114
--> %:x : NULL
115+
--> https://example.com:0/ : NULL
115116
--> http:///blah.com : bool(false)
116117
--> http://:80 : bool(false)
117118
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_basic_005.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ echo "Done";
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114114
--> %:x : NULL
115+
--> https://example.com:0/ : NULL
115116
--> http:///blah.com : bool(false)
116117
--> http://:80 : bool(false)
117118
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_basic_006.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ echo "Done";
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114114
--> %:x : NULL
115+
--> https://example.com:0/ : NULL
115116
--> http:///blah.com : bool(false)
116117
--> http://:80 : bool(false)
117118
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_basic_007.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ echo "Done";
112112
--> / : string(1) "/"
113113
--> /rest/Users?filter={"id":"123"} : string(11) "/rest/Users"
114114
--> %:x : string(3) "%:x"
115+
--> https://example.com:0/ : string(1) "/"
115116
--> http:///blah.com : bool(false)
116117
--> http://:80 : bool(false)
117118
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_basic_008.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ echo "Done";
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : string(19) "filter={"id":"123"}"
114114
--> %:x : NULL
115+
--> https://example.com:0/ : NULL
115116
--> http:///blah.com : bool(false)
116117
--> http://:80 : bool(false)
117118
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_basic_009.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ echo "Done";
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114114
--> %:x : NULL
115+
--> https://example.com:0/ : NULL
115116
--> http:///blah.com : bool(false)
116117
--> http://:80 : bool(false)
117118
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_unterminated.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,15 @@ echo "Done";
861861
string(3) "%:x"
862862
}
863863

864+
--> https://example.com:0/: array(3) {
865+
["scheme"]=>
866+
string(5) "https"
867+
["host"]=>
868+
string(11) "example.com"
869+
["path"]=>
870+
string(1) "/"
871+
}
872+
864873
--> http:///blah.com: bool(false)
865874

866875
--> http://:80: bool(false)

ext/standard/tests/url/urls.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ $urls = array(
9292
'/',
9393
'/rest/Users?filter={"id":"123"}',
9494
'%:x',
95+
'https://example.com:0/',
9596

9697
// Severely malformed URLs that do not parse:
9798
'http:///blah.com',

ext/standard/url.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,11 @@ PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
194194

195195
if (pp - p > 0 && pp - p < 6 && (pp == ue || *pp == '/')) {
196196
zend_long port;
197+
char *end;
197198
memcpy(port_buf, p, (pp - p));
198199
port_buf[pp - p] = '\0';
199-
port = ZEND_STRTOL(port_buf, NULL, 10);
200-
if (port > 0 && port <= 65535) {
200+
port = ZEND_STRTOL(port_buf, &end, 10);
201+
if (port >= 0 && port <= 65535 && end != port_buf) {
201202
ret->port = (unsigned short) port;
202203
if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
203204
s += 2;
@@ -258,10 +259,11 @@ PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
258259
return NULL;
259260
} else if (e - p > 0) {
260261
zend_long port;
262+
char *end;
261263
memcpy(port_buf, p, (e - p));
262264
port_buf[e - p] = '\0';
263-
port = ZEND_STRTOL(port_buf, NULL, 10);
264-
if (port > 0 && port <= 65535) {
265+
port = ZEND_STRTOL(port_buf, &end, 10);
266+
if (port >= 0 && port <= 65535 && end != port_buf) {
265267
ret->port = (unsigned short)port;
266268
} else {
267269
php_url_free(ret);

0 commit comments

Comments
 (0)