Repository navigation
Conversation
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>
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>
vstinner
reviewed
Nov 30, 2026
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
reviewed
Nov 30, 2026
vstinner
left a comment
Member
There was a problem hiding this comment.
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()
| # 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) |
Member
There was a problem hiding this comment.
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) |
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>
picnixz
reviewed
Nov 30, 2026
vstinner
approved these changes
Nov 30, 2026
vstinner
left a comment
Member
There was a problem hiding this comment.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.