]> granicus.if.org Git - php/commitdiff
Fix #80114: parse_url does not accept URLs with port 0
authorChristoph M. Becker <cmbecker69@gmx.de>
Sun, 20 Sep 2020 11:45:09 +0000 (13:45 +0200)
committerChristoph M. Becker <cmbecker69@gmx.de>
Sun, 20 Sep 2020 13:34:45 +0000 (15:34 +0200)
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.

13 files changed:
NEWS
ext/standard/tests/url/parse_url_basic_001.phpt
ext/standard/tests/url/parse_url_basic_002.phpt
ext/standard/tests/url/parse_url_basic_003.phpt
ext/standard/tests/url/parse_url_basic_004.phpt
ext/standard/tests/url/parse_url_basic_005.phpt
ext/standard/tests/url/parse_url_basic_006.phpt
ext/standard/tests/url/parse_url_basic_007.phpt
ext/standard/tests/url/parse_url_basic_008.phpt
ext/standard/tests/url/parse_url_basic_009.phpt
ext/standard/tests/url/parse_url_unterminated.phpt
ext/standard/tests/url/urls.inc
ext/standard/url.c

diff --git a/NEWS b/NEWS
index fe2bae3803352b63d171d8f225ee7cc9b9bf3790..98dd5861148cd5641f06f2a4a4144925b7c84e0f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,9 @@ PHP                                                                        NEWS
   . Fixed bug #80083 (Optimizer pass 6 removes variables used for ibm_db2 data
     binding). (Nikita)
 
+- Standard:
+  . Fixed bug #80114 (parse_url does not accept URLs with port 0). (cmb, twosee)
+
 01 Oct 2020, PHP 7.3.23
 
 - Core:
index 51bae9fe1ca448c01c84523be59e3676703385b5..063fc28832fcc975101d787b4bb84a6df9ddc8da 100644 (file)
@@ -859,6 +859,15 @@ echo "Done";
   string(3) "%:x"
 }
 
+--> https://example.com:0/: array(3) {
+  ["scheme"]=>
+  string(5) "https"
+  ["host"]=>
+  string(11) "example.com"
+  ["path"]=>
+  string(1) "/"
+}
+
 --> http:///blah.com: bool(false)
 
 --> http://:80: bool(false)
index 309c038794c45ddf499527ae85e90042c4aab955..42a8457431e75152553a5d4174f541bbc5f97f92 100644 (file)
@@ -113,6 +113,7 @@ echo "Done";
 --> /   : NULL
 --> /rest/Users?filter={"id":"123"}   : NULL
 --> %:x   : NULL
+--> https://example.com:0/   : string(5) "https"
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index 9649bdadb12380e4de1dc8e071cc797b906fbd01..8b0e7eb87500ab20d8e3fb76afe363bbbd456ca0 100644 (file)
@@ -112,6 +112,7 @@ echo "Done";
 --> /   : NULL
 --> /rest/Users?filter={"id":"123"}   : NULL
 --> %:x   : NULL
+--> https://example.com:0/   : string(11) "example.com"
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index 75aacdf847ada4ab5f3aa9f92fc441fe95b70aff..042daefeda8d95f9593f871428caf5119152d227 100644 (file)
@@ -112,6 +112,7 @@ echo "Done";
 --> /   : NULL
 --> /rest/Users?filter={"id":"123"}   : NULL
 --> %:x   : NULL
+--> https://example.com:0/   : NULL
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index 1463e0a29ae04924234d89e18fb8ece731fddfd9..a5ca381a691fe7b0e099b6c558b871f4a025e3e4 100644 (file)
@@ -112,6 +112,7 @@ echo "Done";
 --> /   : NULL
 --> /rest/Users?filter={"id":"123"}   : NULL
 --> %:x   : NULL
+--> https://example.com:0/   : NULL
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index 78eee265ce4df3583f109f129d7e95aede0f8453..51dcb2018cd881643c289e4df157977daae99c8e 100644 (file)
@@ -112,6 +112,7 @@ echo "Done";
 --> /   : NULL
 --> /rest/Users?filter={"id":"123"}   : NULL
 --> %:x   : NULL
+--> https://example.com:0/   : NULL
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index 85a420c88c91009020adf1525c9bb53f286d5e8e..28a6d154f99886d43e994d9f25f70943d1253990 100644 (file)
@@ -112,6 +112,7 @@ echo "Done";
 --> /   : string(1) "/"
 --> /rest/Users?filter={"id":"123"}   : string(11) "/rest/Users"
 --> %:x   : string(3) "%:x"
+--> https://example.com:0/   : string(1) "/"
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index 75952b2ecd7322debf2cedcc0aa964d4e307f324..c2adc9e0700bff6deecbe16b49630ce7126b416f 100644 (file)
@@ -112,6 +112,7 @@ echo "Done";
 --> /   : NULL
 --> /rest/Users?filter={"id":"123"}   : string(19) "filter={"id":"123"}"
 --> %:x   : NULL
+--> https://example.com:0/   : NULL
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index ab9232a9a7dd058ba34f291d704002a874df849a..3074a08347c8c4b0d3082eb26651f9cba0f446c5 100644 (file)
@@ -112,6 +112,7 @@ echo "Done";
 --> /   : NULL
 --> /rest/Users?filter={"id":"123"}   : NULL
 --> %:x   : NULL
+--> https://example.com:0/   : NULL
 --> http:///blah.com   : bool(false)
 --> http://:80   : bool(false)
 --> http://user@:80   : bool(false)
index 6a0cf02745354b358da9883f7a605eadaeb3098a..8af50dbe287fe4e8c5ed4b0bd2abbbb38dd1abda 100644 (file)
@@ -861,6 +861,15 @@ echo "Done";
   string(3) "%:x"
 }
 
+--> https://example.com:0/: array(3) {
+  ["scheme"]=>
+  string(5) "https"
+  ["host"]=>
+  string(11) "example.com"
+  ["path"]=>
+  string(1) "/"
+}
+
 --> http:///blah.com: bool(false)
 
 --> http://:80: bool(false)
index 199f22caea1d387cc136c01c13471b5177eba82d..d334f4e9ab2be585d63d1a4a9368f5a70cbf959f 100644 (file)
@@ -92,6 +92,7 @@ $urls = array(
 '/',
 '/rest/Users?filter={"id":"123"}',
 '%:x',
+'https://example.com:0/',
 
 // Severely malformed URLs that do not parse:
 'http:///blah.com',
index 7763759bc1d0b4cc04ff681993bbe68da73aeee2..fde4ff53779677cb7c96803f689a1f1a15534724 100644 (file)
@@ -194,10 +194,11 @@ PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
 
                if (pp - p > 0 && pp - p < 6 && (pp == ue || *pp == '/')) {
                        zend_long port;
+                       char *end;
                        memcpy(port_buf, p, (pp - p));
                        port_buf[pp - p] = '\0';
-                       port = ZEND_STRTOL(port_buf, NULL, 10);
-                       if (port > 0 && port <= 65535) {
+                       port = ZEND_STRTOL(port_buf, &end, 10);
+                       if (port >= 0 && port <= 65535 && end != port_buf) {
                                ret->port = (unsigned short) port;
                                if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
                                    s += 2;
@@ -258,10 +259,11 @@ parse_host:
                                return NULL;
                        } else if (e - p > 0) {
                                zend_long port;
+                               char *end;
                                memcpy(port_buf, p, (e - p));
                                port_buf[e - p] = '\0';
-                               port = ZEND_STRTOL(port_buf, NULL, 10);
-                               if (port > 0 && port <= 65535) {
+                               port = ZEND_STRTOL(port_buf, &end, 10);
+                               if (port >= 0 && port <= 65535 && end != port_buf) {
                                        ret->port = (unsigned short)port;
                                } else {
                                        php_url_free(ret);