Skip to content

Commit 2aef2ba

Browse files
committed
gh-162375: Enforce format width and precision limits in _pydecimal
Enforce MAX_PREC limits on minimumwidth and precision in _pydecimal._parse_format_specifier() to match _decimal and prevent unbounded CPU and memory usage on excessive specifiers. Also optimize _pydecimal._insert_thousands_sep() when thousands_sep is empty by padding directly in one step rather than looping in groups of 3.
1 parent 91c4c6b commit 2aef2ba

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎Lib/_pydecimal.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6323,8 +6323,12 @@ def _parse_format_specifier(format_spec, _localeconv=None):
63236323

63246324
# minimumwidth defaults to 0; precision remains None if not given
63256325
format_dict['minimumwidth'] = int(format_dict['minimumwidth'] or '0')
6326+
if format_dict['minimumwidth'] > MAX_PREC:
6327+
raise ValueError("format specification exceeds internal limits of _decimal")
63266328
if format_dict['precision'] is not None:
63276329
format_dict['precision'] = int(format_dict['precision'])
6330+
if format_dict['precision'] > MAX_PREC:
6331+
raise ValueError("format specification exceeds internal limits of _decimal")
63286332

63296333
# if format type is 'g' or 'G' then a precision of 0 makes little
63306334
# sense; convert it to 1. Same if format type is unspecified.
@@ -6424,6 +6428,12 @@ def _insert_thousands_sep(digits, spec, min_width=1):
64246428
"""
64256429

64266430
sep = spec['thousands_sep']
6431+
if not sep:
6432+
pad = max(min_width, 1 if not digits else 0) - len(digits)
6433+
if pad > 0:
6434+
return '0' * pad + digits
6435+
return digits
6436+
64276437
grouping = spec['grouping']
64286438

64296439
groups = []

‎Lib/test/test_decimal.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,20 @@ def __init__(self, a):
13261326
a = A.from_float(42)
13271327
self.assertEqual(self.decimal.Decimal, a.a_type)
13281328

1329+
def test_format_limits(self):
1330+
Decimal = self.decimal.Decimal
1331+
limit = self.decimal.MAX_PREC + 1
1332+
with self.assertRaises(ValueError):
1333+
format(Decimal('1.5'), f'{limit}')
1334+
with self.assertRaises(ValueError):
1335+
format(Decimal('1.5'), f'.{limit}f')
1336+
with self.assertRaises(ValueError):
1337+
format(Decimal('1.5'), f'#0{limit}')
1338+
# Verify that large padding without thousands separator formats correctly
1339+
res = format(Decimal('1.5'), '#01000')
1340+
self.assertEqual(len(res), 1000)
1341+
self.assertTrue(res.startswith('000') and res.endswith('1.5'))
1342+
13291343
@requires_cdecimal
13301344
class CFormatTest(FormatTest, unittest.TestCase):
13311345
decimal = C
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Enforce format specification width and precision limits in :mod:`_pydecimal`
2+
to match :mod:`_decimal` and avoid excessive resource consumption on huge widths.

0 commit comments

Comments
 (0)