Skip to content

Commit 96c4d8b

Browse files
authored
uri: Fix handling of empty ports for uri_parser_rfc3986 (php#19645)
* uri: Fix handling of empty ports for uri_parser_rfc3986 * NEWS * uri: Skip the port validation during parsing when port component is empty
1 parent 34a6e86 commit 96c4d8b

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ PHP NEWS
6666
the Uri\WhatWg\Url parser. (timwolla)
6767
. Reject out-of-range ports when using the Uri\Rfc3986\Uri parser.
6868
(timwolla)
69+
. Return null instead of 0 for Uri\Rfc3986\Uri::getPort() when the
70+
URI contains an empty port. (timwolla)
6971
. Clean up naming of internal API. (timwolla)
7072

7173
28 Aug 2025, PHP 8.5.0beta2

ext/uri/tests/059.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Test empty ports become null
3+
--EXTENSIONS--
4+
uri
5+
--FILE--
6+
<?php
7+
8+
$uri = new \Uri\Rfc3986\Uri('https://example.com:');
9+
var_dump($uri, $uri->getPort());
10+
11+
?>
12+
--EXPECTF--
13+
object(Uri\Rfc3986\Uri)#%d (8) {
14+
["scheme"]=>
15+
string(5) "https"
16+
["username"]=>
17+
NULL
18+
["password"]=>
19+
NULL
20+
["host"]=>
21+
string(11) "example.com"
22+
["port"]=>
23+
NULL
24+
["path"]=>
25+
string(0) ""
26+
["query"]=>
27+
NULL
28+
["fragment"]=>
29+
NULL
30+
}
31+
NULL

ext/uri/uri_parser_rfc3986.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_port_read(const
212212
{
213213
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
214214

215-
if (has_text_range(&uriparser_uri->portText)) {
215+
if (has_text_range(&uriparser_uri->portText) && get_text_range_length(&uriparser_uri->portText) > 0) {
216216
ZVAL_LONG(retval, port_str_to_zend_long_checked(uriparser_uri->portText.first, get_text_range_length(&uriparser_uri->portText)));
217217
} else {
218218
ZVAL_NULL(retval);
@@ -326,15 +326,14 @@ php_uri_parser_rfc3986_uris *php_uri_parser_rfc3986_parse_ex(const char *uri_str
326326
/* Make the resulting URI independent of the 'uri_str'. */
327327
uriMakeOwnerMmA(&uri, mm);
328328

329-
if (
330-
has_text_range(&uri.portText)
331-
&& port_str_to_zend_long_checked(uri.portText.first, get_text_range_length(&uri.portText)) == -1
332-
) {
333-
if (!silent) {
334-
zend_throw_exception(uri_invalid_uri_exception_ce, "The port is out of range", 0);
335-
}
329+
if (has_text_range(&uri.portText) && get_text_range_length(&uri.portText) > 0) {
330+
if (port_str_to_zend_long_checked(uri.portText.first, get_text_range_length(&uri.portText)) == -1) {
331+
if (!silent) {
332+
zend_throw_exception(uri_invalid_uri_exception_ce, "The port is out of range", 0);
333+
}
336334

337-
goto fail;
335+
goto fail;
336+
}
338337
}
339338

340339
php_uri_parser_rfc3986_uris *uriparser_uris = uriparser_create_uris();

0 commit comments

Comments
 (0)