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>
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
left a comment
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) |
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.
| 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>
vstinner
left a comment
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>
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.