Fixed #33368 -- Fixed parse_duration() crash on invalid separators for decimal fractions.

This commit is contained in:
mendespedro 2021-12-15 13:56:04 -03:00 committed by Mariusz Felisiak
parent b0d16d0129
commit 4fd3044ca0
3 changed files with 11 additions and 4 deletions

View File

@ -42,11 +42,11 @@ standard_duration_re = _lazy_re_compile(
iso8601_duration_re = _lazy_re_compile( iso8601_duration_re = _lazy_re_compile(
r'^(?P<sign>[-+]?)' r'^(?P<sign>[-+]?)'
r'P' r'P'
r'(?:(?P<days>\d+(.\d+)?)D)?' r'(?:(?P<days>\d+([\.,]\d+)?)D)?'
r'(?:T' r'(?:T'
r'(?:(?P<hours>\d+(.\d+)?)H)?' r'(?:(?P<hours>\d+([\.,]\d+)?)H)?'
r'(?:(?P<minutes>\d+(.\d+)?)M)?' r'(?:(?P<minutes>\d+([\.,]\d+)?)M)?'
r'(?:(?P<seconds>\d+(.\d+)?)S)?' r'(?:(?P<seconds>\d+([\.,]\d+)?)S)?'
r')?' r')?'
r'$' r'$'
) )

View File

@ -30,6 +30,8 @@ class DurationFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
msg = 'Enter a valid duration.' msg = 'Enter a valid duration.'
with self.assertRaisesMessage(ValidationError, msg): with self.assertRaisesMessage(ValidationError, msg):
f.clean('not_a_time') f.clean('not_a_time')
with self.assertRaisesMessage(ValidationError, msg):
DurationField().clean('P3(3D')
def test_durationfield_clean_not_required(self): def test_durationfield_clean_not_required(self):
f = DurationField(required=False) f = DurationField(required=False)

View File

@ -161,6 +161,11 @@ class DurationParseTests(unittest.TestCase):
('-PT0.000005S', timedelta(microseconds=-5)), ('-PT0.000005S', timedelta(microseconds=-5)),
('-PT0,000005S', timedelta(microseconds=-5)), ('-PT0,000005S', timedelta(microseconds=-5)),
('-P4DT1H', timedelta(days=-4, hours=-1)), ('-P4DT1H', timedelta(days=-4, hours=-1)),
# Invalid separators for decimal fractions.
('P3(3D', None),
('PT3)3H', None),
('PT3|3M', None),
('PT3/3S', None),
) )
for source, expected in test_values: for source, expected in test_values:
with self.subTest(source=source): with self.subTest(source=source):