mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
bpo-40480 "fnmatch" exponential execution time (GH-19908)
bpo-40480: create different regexps in the presence of multiple `*` patterns to prevent fnmatch() from taking exponential time.
This commit is contained in:
parent
96074de573
commit
b9c46a2c2d
3 changed files with 71 additions and 7 deletions
|
|
@ -45,6 +45,13 @@ class FnmatchTestCase(unittest.TestCase):
|
|||
check('\nfoo', 'foo*', False)
|
||||
check('\n', '*')
|
||||
|
||||
def test_slow_fnmatch(self):
|
||||
check = self.check_match
|
||||
check('a' * 50, '*a*a*a*a*a*a*a*a*a*a')
|
||||
# The next "takes forever" if the regexp translation is
|
||||
# straightforward. See bpo-40480.
|
||||
check('a' * 50 + 'b', '*a*a*a*a*a*a*a*a*a*a', False)
|
||||
|
||||
def test_mix_bytes_str(self):
|
||||
self.assertRaises(TypeError, fnmatch, 'test', b'*')
|
||||
self.assertRaises(TypeError, fnmatch, b'test', '*')
|
||||
|
|
@ -107,6 +114,16 @@ class TranslateTestCase(unittest.TestCase):
|
|||
self.assertEqual(translate('[!x]'), r'(?s:[^x])\Z')
|
||||
self.assertEqual(translate('[^x]'), r'(?s:[\^x])\Z')
|
||||
self.assertEqual(translate('[x'), r'(?s:\[x)\Z')
|
||||
# from the docs
|
||||
self.assertEqual(translate('*.txt'), r'(?s:.*\.txt)\Z')
|
||||
# squash consecutive stars
|
||||
self.assertEqual(translate('*********'), r'(?s:.*)\Z')
|
||||
self.assertEqual(translate('A*********'), r'(?s:A.*)\Z')
|
||||
self.assertEqual(translate('*********A'), r'(?s:.*A)\Z')
|
||||
self.assertEqual(translate('A*********?[?]?'), r'(?s:A.*.[?].)\Z')
|
||||
# fancy translation to prevent exponential-time match failure
|
||||
self.assertEqual(translate('**a*a****a'),
|
||||
r'(?s:(?=(?P<g1>.*?a))(?P=g1)(?=(?P<g2>.*?a))(?P=g2).*a)\Z')
|
||||
|
||||
|
||||
class FilterTestCase(unittest.TestCase):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue