gh-148370: prevent quadratic behavior in `configparser.ParsingError.c… · python/cpython@2662db0 · GitHub
Skip to content

Commit 2662db0

Browse files
authored
gh-148370: prevent quadratic behavior in configparser.ParsingError.combine (#148452)
1 parent 289fd2c commit 2662db0

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

Lib/configparser.py

Lines changed: 6 additions & 3 deletions

Lib/test/test_configparser.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,19 @@ def test_error(self):
17291729
self.assertEqual(e1.message, e2.message)
17301730
self.assertEqual(repr(e1), repr(e2))
17311731

1732+
def test_combine_error_linear_complexity(self):
1733+
# Ensure that ParsingError.combine() has linear complexity.
1734+
# See https://github.com/python/cpython/issues/148370.
1735+
n = 50000
1736+
s = '[*]\n' + (err_line := '=\n') * n
1737+
p = configparser.ConfigParser(strict=False)
1738+
with self.assertRaises(configparser.ParsingError) as cm:
1739+
p.read_string(s)
1740+
errlines = cm.exception.message.splitlines()
1741+
self.assertEqual(len(errlines), n + 1)
1742+
self.assertStartsWith(errlines[0], "Source contains parsing errors: ")
1743+
self.assertEqual(errlines[42], f"\t[line {43:2d}]: {err_line!r}")
1744+
17321745
def test_nosectionerror(self):
17331746
import pickle
17341747
e1 = configparser.NoSectionError('section')
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)