Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,11 @@ def parse227(resp):
global _227_re
if _227_re is None:
import re
_227_re = re.compile(r'(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)', re.ASCII)
# The negative lookbehind keeps the search from retrying at every
# digit of a long run, which made parsing quadratic for replies
# that do not contain six comma-separated numbers (gh-162867).
_227_re = re.compile(r'(?<!\d)(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)',
re.ASCII)
m = _227_re.search(resp)
if not m:
raise error_proto(resp)
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,22 @@ def test__all__(self):
'ftpcp', 'test'}
support.check__all__(self, ftplib, not_exported=not_exported)

def test_parse227_long_octet(self):
# A digit boundary guard must not change the result for valid replies.
octet = '9' * 1000
self.assertEqual(
ftplib.parse227(f'227 Entering Passive Mode ({octet},2,3,4,5,6)'),
(f'{octet}.2.3.4', 1286))

def test_parse227_malformed_long_reply_is_linear(self):
# gh-162867: without a boundary guard the regex engine retried the
# match at every digit of a long run, making parsing quadratic.
resp = '227 ' + '1' * 50_000
start = time.perf_counter()
with self.assertRaises(ftplib.error_proto):
ftplib.parse227(resp)
self.assertLess(time.perf_counter() - start, 5.0)


class CommandLineTest(TestCase):
"""Tests for the ftplib command-line interface."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix quadratic parsing time in :func:`ftplib.parse227` for malformed replies
containing a long run of digits.
Loading