Skip to content

gh-162867: Fix quadratic parsing time in ftplib.parse227() - #162873

Open
tozkdeha wants to merge 6 commits into
python:mainfrom
tozkdeha:fix-162867
Open

tozkdeha wants to merge 6 commits into
python:mainfrom
tozkdeha:fix-162867

Conversation

@tozkdeha

@tozkdeha tozkdeha commented Nov 30, 2026 •

Copy link
Copy Markdown

parse227() searched the PASV reply with an unbounded r'(\d+),...' regex. On a reply made of many digits without a valid (h1,h2,h3,h4,p1,p2) tuple, re.search() backtracked at every start position, so parsing time grew quadratically with the length of the reply.

Each field is an unsigned 8-bit value (0..255), so bound every group to at most three digits. Parsing is now linear in the length of the reply and the result for valid replies is unchanged.

parse227() searched the PASV reply with an unbounded r'(\d+),...' regex.
On a reply made of many digits without a valid (h1,h2,h3,h4,p1,p2) tuple,
re.search() backtracked at every start position, so parsing time grew
quadratically with the length of the reply.

Each field is an unsigned 8-bit value (0..255), so bound every group to at
most three digits. Parsing is now linear in the length of the reply and the
result for valid replies is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tozkdeha
tozkdeha requested a review from giampaolo as a code owner November 30, 2026 14:18
@python-cla-bot

python-cla-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

parse227() is not documented, so :func:`ftplib.parse227` had no cross-reference
target and failed the "Docs" check.  Reference the :mod:`ftplib` module instead.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread Lib/ftplib.py Resolved
Comment thread Lib/test/test_ftplib.py Resolved
Comment thread Lib/test/test_ftplib.py Resolved
Per review by Victor Stinner:
- build the regex from a reusable field pattern and anchor the first
  field with \b, so a field longer than three digits (e.g. 1234) is
  rejected instead of being silently truncated;
- separate the valid and invalid cases in the test with a blank line;
- add tests for a missing field and for fields larger than 255.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vstinner vstinner added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 30, 2026

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to move the following parsing tests to MiscTests? Currently, tests are run twice which is not needed: once for FTP, once for FTP+TLS.

  • test_parse257()
  • test_parse227()
  • test_parse227_linear_time()
Comment thread Lib/test/test_ftplib.py Outdated
# must stay fast and still raise error_proto; should the regression
# return, this test hangs instead of completing.
resp = '227 ' + '9' * 1_000_000
self.assertRaises(ftplib.error_proto, ftplib.parse227, resp)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum. I think that we should put some bounds to the maximum execution time. I suggest SHORT_TIMEOUT (30 seconds by default). parse227() takes 8 ms on my laptop with Python built in debug mode (gcc -Og): SHORT_TIMEOUT is 3750x larger than timing, so it should not fail if the machine is busy / too slow. Anyway, if later this limit becomes an issue, we can extend it to LONG_TIMEOUT.

Suggested change
self.assertRaises(ftplib.error_proto, ftplib.parse227, resp)
start_time = time.perf_counter()
self.assertRaises(ftplib.error_proto, ftplib.parse227, resp)
# The function usually takes less than a second
self.assertLessEqual(time.perf_counter() - start_time,
support.SHORT_TIMEOUT)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course.

tozkdeha and others added 2 commits November 30, 2026 19:02
Follow Victor Stinner's review: instead of relying on the test hanging
should the quadratic-time regression return, bound the parsing time to
support.SHORT_TIMEOUT so the test fails fast and explicitly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Follow Victor Stinner's review: test_parse257(), test_parse227() and
test_parse227_linear_time() only exercise module-level parsing helpers
and do not need a client/server. They lived in TestFTPClass, which is
reused by TestTLS_FTPClassMixin, so they ran twice (plain FTP and
FTP+TLS). Moving them to MiscTestCase runs them once.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread Lib/ftplib.py Resolved

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the updates.

Follow Bénédikt Tran's review: construct the _227_re pattern with
','.join([field] * 6) instead of field + (',' + field) * 5. The compiled
pattern is identical; this is only clearer to read.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes

3 participants