Title: _pydecimal fallback skips _decimal's format width limit: format(Decimal, '#0999999999999999999') hangs/OOMs where the C implementation rejects instantly · Issue #162375 · python/cpython · GitHub
Open Graph Title: _pydecimal fallback skips _decimal's format width limit: format(Decimal, '#0999999999999999999') hangs/OOMs where the C implementation rejects instantly · Issue #162375 · python/cpython
X Title: _pydecimal fallback skips _decimal's format width limit: format(Decimal, '#0999999999999999999') hangs/OOMs where the C implementation rejects instantly · Issue #162375 · python/cpython
Description: Bug report Per the closed security advisory GHSA-7qmv-vvx2-jcfh - PSRT asked me to refile this as a regular issue, so here it is with the security framing dropped. The impact discussed here is resource consumption only. What happens from...
Open Graph Description: Bug report Per the closed security advisory GHSA-7qmv-vvx2-jcfh - PSRT asked me to refile this as a regular issue, so here it is with the security framing dropped. The impact discussed here is reso...
X Description: Bug report Per the closed security advisory GHSA-7qmv-vvx2-jcfh - PSRT asked me to refile this as a regular issue, so here it is with the security framing dropped. The impact discussed here is reso...
Opengraph URL: https://github.com/python/cpython/issues/162375
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_pydecimal fallback skips _decimal's format width limit: format(Decimal, '#0999999999999999999') hangs/OOMs where the C implementation rejects instantly","articleBody":"### Bug report\n\nPer the closed security advisory GHSA-7qmv-vvx2-jcfh - PSRT asked me to refile this as a regular issue, so here it is with the security framing dropped. The impact discussed here is resource consumption only.\n\n**What happens**\n\n```python\nfrom decimal import Decimal\nformat(Decimal('1.5'), '#0999999999999999999')\n```\n\nThis never returns. One CPU core sits at 100% and memory keeps growing until the process is OOM-killed. The spec is only 20 characters. The C `_decimal` implementation rejects the exact same spec instantly with \"format specification exceeds internal limits of _decimal\", and `int`/`float` also fail right away with MemoryError for equivalent widths.\n\n**Why**\n\ngh-114563 / GH-114879 added a fallback: when libmpdec's `mpd_parse_fmt_str()` cannot parse a spec, `dec_format()` retries it through `_pydecimal.Decimal.__format__` via `pydec_format()`. libmpdec doesn't accept the `#` flag, so any spec containing `#` takes that path (same for `z` with libmpdec 2.5.x, and for a leading-`0` width like `00999...` on 3.14). `_pydecimal` has no limit on `minimumwidth`, and with zero padding `_insert_thousands_sep()` uses grouping `[3, 0]` even when there is no separator, appending `'000'` one Python loop iteration and one string object per group - O(width), so a width near 10**18 never finishes. With memory capped at 1 GiB it ran at 100% CPU for about 13 seconds before hitting MemoryError.\n\n**Suggested fix**\n\nApply the same width/precision limit (`MAX_PREC`) that the C side enforces in `pydec_format()` or `_pydecimal.__format__`, raising ValueError before formatting. A cheaper partial improvement: make `_insert_thousands_sep()` pad once when there is no separator, instead of looping per group.\n\nGuard parity with the C implementation is the point - and there is precedent for this class of guard: the int-to-str digit limit added for gh-95778 (CVE-2020-10735) exists precisely to cap conversions that can produce enormous output strings.\n\n**Versions tested** (all hang with the repro above unless noted)\n\n- 3.14.3, official python.org Windows build (libmpdec 4.0.0): `#` and `00` hang; `z` does not (libmpdec 4 parses `z` itself)\n- 3.14.7, Debian/Kali package (libmpdec 2.5.1): `#`, `z` and `00` all hang\n- 3.14.10, built from source (libmpdec 2.5.1): `#`, `z` and `00` all hang\n- 3.12.3 (source build) and 3.12 branch head (3.12.16+): `#` and `z` hang; `00` gives ValueError on 3.12 because the regex there has `(?!0)`\n\n**Impact**\n\nAvailability only. An app that applies a user-chosen format spec to a Decimal (report builders, export/locale settings, templates exposing format specs, `f\"{price:{user_spec}}\"`) ties up a worker until it is OOM-killed, and a few parallel requests can take the service down. Apps that only use their own fixed format specs are not affected. Not claiming memory corruption or code execution.\n\n### CPython versions tested on:\n\n3.12, 3.14\n\n### Operating systems tested on:\n\nLinux, Windows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162380\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kksecure","@type":"Person","name":"kksecure"},"datePublished":"2026-11-21T09:54:21Z","url":"https://github.com/python/cpython/issues/162375"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4ba60ac4-7727-a6bc-1f7e-1ef5cea7be69 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A646:2F543F:1234012:181957E:6B01E93E |
| html-safe-nonce | 3f751640364be48c2fadd64fa4159ecb199f8498a94fe06ad0a8a42a379c7aae |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjQ2OjJGNTQzRjoxMjM0MDEyOjE4MTk1N0U6NkIwMUU5M0UiLCJ2aXNpdG9yX2lkIjoiODI3NTg0NjE0Nzk5NDY3NTUxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 993a5177c07f4835632a182e225f2a487d704b9791fc438a499ffc7dbace9bb4 |
| hovercard-subject-tag | issue:6527322458 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/162375/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d4562849f9d5ce1df80ad5d4aad2d936d8156d0b054f160efd0d410e3ec204f2/python/cpython/issues/162375 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d4562849f9d5ce1df80ad5d4aad2d936d8156d0b054f160efd0d410e3ec204f2/python/cpython/issues/162375 |
| og:image:alt | Bug report Per the closed security advisory GHSA-7qmv-vvx2-jcfh - PSRT asked me to refile this as a regular issue, so here it is with the security framing dropped. The impact discussed here is reso... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kksecure |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width