Skip to content

_pydecimal fallback skips _decimal's format width limit: format(Decimal, '#0999999999999999999') hangs/OOMs where the C implementation rejects instantly #162375

Open

Description

@kksecure

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 decimal import Decimal
format(Decimal('1.5'), '#0999999999999999999')

This 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.

Why

gh-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.

Suggested fix

Apply 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.

Guard 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.

Versions tested (all hang with the repro above unless noted)

  • 3.14.3, official python.org Windows build (libmpdec 4.0.0): # and 00 hang; z does not (libmpdec 4 parses z itself)
  • 3.14.7, Debian/Kali package (libmpdec 2.5.1): #, z and 00 all hang
  • 3.14.10, built from source (libmpdec 2.5.1): #, z and 00 all hang
  • 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)

Impact

Availability 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.

CPython versions tested on:

3.12, 3.14

Operating systems tested on:

Linux, Windows

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions