From 2aef2bab5d6516fdad595298bc2b32f6db7e23dd Mon Sep 17 00:00:00 2001 From: asisec Date: Sat, 21 Nov 2026 14:00:50 +0300 Subject: [PATCH 1/2] 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. --- Lib/_pydecimal.py | 10 ++++++++++ Lib/test/test_decimal.py | 14 ++++++++++++++ .../2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst | 2 ++ 3 files changed, 26 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index 4b7bfd07887bd3..5b9614bc0723b8 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -6323,8 +6323,12 @@ def _parse_format_specifier(format_spec, _localeconv=None): # minimumwidth defaults to 0; precision remains None if not given format_dict['minimumwidth'] = int(format_dict['minimumwidth'] or '0') + if format_dict['minimumwidth'] > MAX_PREC: + raise ValueError("format specification exceeds internal limits of _decimal") if format_dict['precision'] is not None: format_dict['precision'] = int(format_dict['precision']) + if format_dict['precision'] > MAX_PREC: + raise ValueError("format specification exceeds internal limits of _decimal") # if format type is 'g' or 'G' then a precision of 0 makes little # sense; convert it to 1. Same if format type is unspecified. @@ -6424,6 +6428,12 @@ def _insert_thousands_sep(digits, spec, min_width=1): """ sep = spec['thousands_sep'] + if not sep: + pad = max(min_width, 1 if not digits else 0) - len(digits) + if pad > 0: + return '0' * pad + digits + return digits + grouping = spec['grouping'] groups = [] diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index ef7aa5358b5cc6..bfa6938dc5f10e 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -1326,6 +1326,20 @@ def __init__(self, a): a = A.from_float(42) self.assertEqual(self.decimal.Decimal, a.a_type) + def test_format_limits(self): + Decimal = self.decimal.Decimal + limit = self.decimal.MAX_PREC + 1 + with self.assertRaises(ValueError): + format(Decimal('1.5'), f'{limit}') + with self.assertRaises(ValueError): + format(Decimal('1.5'), f'.{limit}f') + with self.assertRaises(ValueError): + format(Decimal('1.5'), f'#0{limit}') + # Verify that large padding without thousands separator formats correctly + res = format(Decimal('1.5'), '#01000') + self.assertEqual(len(res), 1000) + self.assertTrue(res.startswith('000') and res.endswith('1.5')) + @requires_cdecimal class CFormatTest(FormatTest, unittest.TestCase): decimal = C diff --git a/Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst b/Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst new file mode 100644 index 00000000000000..44381996909de2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst @@ -0,0 +1,2 @@ +Enforce format specification width and precision limits in :mod:`_pydecimal` +to match :mod:`_decimal` and avoid excessive resource consumption on huge widths. From ac07d142d058a4173594c8266ea0f46041cf7082 Mon Sep 17 00:00:00 2001 From: asisec Date: Sat, 21 Nov 2026 14:10:18 +0300 Subject: [PATCH 2/2] Fix Sphinx markup in NEWS entry --- .../Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst b/Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst index 44381996909de2..04c928136f5b8e 100644 --- a/Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst +++ b/Misc/NEWS.d/next/Library/2026-11-21-13-59-00.gh-issue-162375.xK9L2P.rst @@ -1,2 +1,2 @@ -Enforce format specification width and precision limits in :mod:`_pydecimal` -to match :mod:`_decimal` and avoid excessive resource consumption on huge widths. +Enforce format specification width and precision limits in ``_pydecimal`` +to match ``_decimal`` and avoid excessive resource consumption on huge widths.