Skip to content

Commit 81811db

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Fix binary-safety of parse_url
2 parents c6fd37c + 54dbd3e commit 81811db

12 files changed

+33
-13
lines changed

ext/standard/tests/url/parse_url_basic_001.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,11 @@ echo "Done";
854854
string(19) "filter={"id":"123"}"
855855
}
856856

857+
--> %:x: array(1) {
858+
["path"]=>
859+
string(3) "%:x"
860+
}
861+
857862
--> http:///blah.com: bool(false)
858863

859864
--> 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
@@ -112,6 +112,7 @@ echo "Done";
112112
--> : NULL
113113
--> / : NULL
114114
--> /rest/Users?filter={"id":"123"} : NULL
115+
--> %:x : 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_003.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ echo "Done";
111111
--> : NULL
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114+
--> %:x : NULL
114115
--> http:///blah.com : bool(false)
115116
--> http://:80 : bool(false)
116117
--> 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
@@ -111,6 +111,7 @@ echo "Done";
111111
--> : NULL
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114+
--> %:x : NULL
114115
--> http:///blah.com : bool(false)
115116
--> http://:80 : bool(false)
116117
--> 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
@@ -111,6 +111,7 @@ echo "Done";
111111
--> : NULL
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114+
--> %:x : NULL
114115
--> http:///blah.com : bool(false)
115116
--> http://:80 : bool(false)
116117
--> 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
@@ -111,6 +111,7 @@ echo "Done";
111111
--> : NULL
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114+
--> %:x : NULL
114115
--> http:///blah.com : bool(false)
115116
--> http://:80 : bool(false)
116117
--> 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
@@ -111,6 +111,7 @@ echo "Done";
111111
--> : string(0) ""
112112
--> / : string(1) "/"
113113
--> /rest/Users?filter={"id":"123"} : string(11) "/rest/Users"
114+
--> %:x : string(3) "%:x"
114115
--> http:///blah.com : bool(false)
115116
--> http://:80 : bool(false)
116117
--> 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
@@ -111,6 +111,7 @@ echo "Done";
111111
--> : NULL
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : string(19) "filter={"id":"123"}"
114+
--> %:x : NULL
114115
--> http:///blah.com : bool(false)
115116
--> http://:80 : bool(false)
116117
--> 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
@@ -111,6 +111,7 @@ echo "Done";
111111
--> : NULL
112112
--> / : NULL
113113
--> /rest/Users?filter={"id":"123"} : NULL
114+
--> %:x : NULL
114115
--> http:///blah.com : bool(false)
115116
--> http://:80 : bool(false)
116117
--> http://user@:80 : bool(false)

ext/standard/tests/url/parse_url_unterminated.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,11 @@ echo "Done";
856856
string(19) "filter={"id":"123"}"
857857
}
858858

859+
--> %:x: array(1) {
860+
["path"]=>
861+
string(3) "%:x"
862+
}
863+
859864
--> http:///blah.com: bool(false)
860865

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

ext/standard/tests/url/urls.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ $urls = array(
9191
'',
9292
'/',
9393
'/rest/Users?filter={"id":"123"}',
94+
'%:x',
9495

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

ext/standard/url.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ PHPAPI php_url *php_url_parse(char const *str)
8787
return php_url_parse_ex(str, strlen(str));
8888
}
8989

90+
static const char *binary_strcspn(const char *s, const char *e, const char *chars) {
91+
while (*chars) {
92+
const char *p = memchr(s, *chars, e - s);
93+
if (p) {
94+
e = p;
95+
}
96+
chars++;
97+
}
98+
return e;
99+
}
100+
90101
/* {{{ php_url_parse
91102
*/
92103
PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
@@ -105,7 +116,7 @@ PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
105116
while (p < e) {
106117
/* scheme = 1*[ lowalpha | digit | "+" | "-" | "." ] */
107118
if (!isalpha(*p) && !isdigit(*p) && *p != '+' && *p != '.' && *p != '-') {
108-
if (e + 1 < ue && e < s + strcspn(s, "?#")) {
119+
if (e + 1 < ue && e < binary_strcspn(s, ue, "?#")) {
109120
goto parse_port;
110121
} else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
111122
s += 2;
@@ -205,18 +216,8 @@ PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
205216
goto just_path;
206217
}
207218

208-
parse_host:
209-
/* Binary-safe strcspn(s, "/?#") */
210-
e = ue;
211-
if ((p = memchr(s, '/', e - s))) {
212-
e = p;
213-
}
214-
if ((p = memchr(s, '?', e - s))) {
215-
e = p;
216-
}
217-
if ((p = memchr(s, '#', e - s))) {
218-
e = p;
219-
}
219+
parse_host:
220+
e = binary_strcspn(s, ue, "/?#");
220221

221222
/* check for login and password */
222223
if ((p = zend_memrchr(s, '@', (e-s)))) {

0 commit comments

Comments
 (0)