[Python-checkins] cpython (2.7): Issue #14036: return None when port in urlparse cross 65535

senthil.kumaran python-checkins at python.org
Thu May 24 15:57:54 CEST 2012


http://hg.python.org/cpython/rev/988903cf24c5
changeset:   77119:988903cf24c5
branch:      2.7
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Thu May 24 21:54:34 2012 +0800
summary:
  Issue #14036: return None when port in urlparse cross 65535

files:
  Lib/test/test_urlparse.py |  5 +++++
  Lib/urlparse.py           |  8 +++++---
  Misc/NEWS                 |  3 +++
  3 files changed, 13 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
--- a/Lib/test/test_urlparse.py
+++ b/Lib/test/test_urlparse.py
@@ -437,6 +437,11 @@
         self.assertEqual(p.port, 80)
         self.assertEqual(p.geturl(), url)
 
+        # Verify an illegal port of value greater than 65535 is set as None
+        url = "http://www.python.org:65536"
+        p = urlparse.urlsplit(url)
+        self.assertEqual(p.port, None)
+
     def test_issue14072(self):
         p1 = urlparse.urlsplit('tel:+31-641044153')
         self.assertEqual(p1.scheme, 'tel')
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
--- a/Lib/urlparse.py
+++ b/Lib/urlparse.py
@@ -97,9 +97,11 @@
         netloc = self.netloc.split('@')[-1].split(']')[-1]
         if ':' in netloc:
             port = netloc.split(':')[1]
-            return int(port, 10)
-        else:
-            return None
+            port = int(port, 10)
+            # verify legal port
+            if (0 <= port <= 65535):
+                return port
+        return None
 
 from collections import namedtuple
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -64,6 +64,9 @@
 Library
 -------
 
+- Issue #14036: Add an additional check to validate that port in urlparse does
+  not go in illegal range and returns None.
+
 - Issue #14888: Fix misbehaviour of the _md5 module when called on data
   larger than 2**32 bytes.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list