Title: Fix process abort on large float format precision by changjoon-park · Pull Request #7633 · RustPython/RustPython · GitHub
Open Graph Title: Fix process abort on large float format precision by changjoon-park · Pull Request #7633 · RustPython/RustPython
X Title: Fix process abort on large float format precision by changjoon-park · Pull Request #7633 · RustPython/RustPython
Description: Summary Formatting a float with a large precision aborts the interpreter instead of raising a Python exception. CPython returns a clean string on the same input. # Before $ ./rustpython -c "print(f'{1.5:.1000000}')" thread 'main' panicked at crates/literal/src/float.rs:135: Formatting argument out of range # → exit 101 (process abort) # After $ ./rustpython -c "print(f'{1.5:.1000000}')" 1.5 Root cause Rust's format!("{:.*}", n, x) macro panics when n exceeds the fmt runtime's internal precision limit. format_fixed in crates/literal/src/float.rs already caps n at u16::MAX before calling format!, but its sibling functions format_general and format_exponent — and the FormatType::Percentage branch in crates/common/src/format.rs — pass user-supplied precision straight through. A one-line user script (f"{x:.N}" with N ≳ 65535) triggers the abort. Affected format types: Type Before After f (fixed) OK (already capped) OK e / E (exponential) abort OK g / G (general) abort OK % (percent) abort OK default (no type) abort (routes to g) OK Fix Add FMT_MAX_PRECISION + clamp_fmt_precision() helper at module level in float.rs. Cap is u16::MAX - 1, not u16::MAX — {:.*e} hits a second assertion (ndigits > 0 in core::num::flt2dec) at exactly u16::MAX; the smaller value covers both {:.*} and {:.*e} uniformly. Apply the helper to: format_fixed (replacing existing ad-hoc cap — consistency only) format_exponent (new, at function entry) format_general (new, at each of three internal format! calls, with saturating arithmetic on derived precision values) FormatType::Percentage branch in common/src/format.rs (new) Complex-number formatting and old-style %-formatting dispatch to the same library functions, so they transitively benefit without separate changes. Why the cap is safe f64 carries only ~17 significant decimal digits. Precision beyond ~17 produces padding zeros (for f/e/%) or is silently trimmed (for g). Capping at ~65K is far beyond any user-meaningful precision and matches the existing format_fixed behavior already shipping in main. Verification $ cargo +1.94.0 build --release Finished `release` profile [optimized] target(s) in 40.54s $ ./target/release/rustpython -m test test_float test_fstring test_format All 3 tests OK. Total tests: run=162 $ ./target/release/rustpython extra_tests/snippets/builtin_format.py # (all assertions pass, including 7 new regression cases) Reliability audit (beyond the trigger path) Probed after the fix: 40 magnitude/type combinations: 10 values × 4 format types at precision 200_000 — 0.0, ±1.5, ±inf, nan, 1e-300, 1e300, f64::MAX, 5e-324. All return clean strings. Boundary precisions: 0 / 1 / 2 for each format type — outputs match expected ('{:.0g}'.format(1.5) == '2', etc.). Complex numbers: five format specs including .200000e — all OK (transitively via format_exponent). Old-style % formatting: '%.200000e' % 1.5 etc. — all OK (transitively via cformat.rs → format_fixed/format_exponent/format_general). Combined format specs: fill + align + width + precision, sign + precision, alternate form + precision, zero-pad + precision, grouping + precision — all OK. Defense-in-depth range: Rust's parser rejects precision > i32::MAX with ValueError("Precision too big"), so the guarded interval [0, i32::MAX] is now panic-free end-to-end. Related Prior fix using the same "cap before Rust format!" approach: format_fixed already did this; this PR extends the same pattern to its siblings. Adjacent hardening PRs using the same spirit (translate native failures into Python exceptions): #7630 (cyclic-AST SIGSEGV → RecursionError), #7632 (deep-JSON SIGSEGV → RecursionError). Summary by CodeRabbit Bug Fixes Float formatting now handles extreme precisions safely: percent formatting outputs "inf%" for infinite values, precision is clamped for fixed/exponential/general formats, and fractional digits are zero-padded when needed to match expected behavior. Tests Added regression tests covering very large precision values across f/e/g/% formats to ensure stable, non-crashing output and preserved NaN/Inf representations.
Open Graph Description: Summary Formatting a float with a large precision aborts the interpreter instead of raising a Python exception. CPython returns a clean string on the same input. # Before $ ./rustpython -c "pr...
X Description: Summary Formatting a float with a large precision aborts the interpreter instead of raising a Python exception. CPython returns a clean string on the same input. # Before $ ./rustpython -c &quo...
Opengraph URL: https://github.com/RustPython/RustPython/pull/7633
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:00998172-0070-ee9f-8099-d30d63517933 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | CACE:136CDA:117BD96:172D1E3:6B01E68C |
| html-safe-nonce | 1e9ee17911919a9bb374defa5086908124eb73e1eab07c5ef6e69a1a684e90ee |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUNFOjEzNkNEQToxMTdCRDk2OjE3MkQxRTM6NkIwMUU2OEMiLCJ2aXNpdG9yX2lkIjoiODQ1MDQ0NjEzMzQ2OTI0MzAyMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 988ed7123f9b76eef0a7f9664c280ce800f0d16a3eaa324afc86cceb45a7e23c |
| hovercard-subject-tag | pull_request:3552436376 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/RustPython/RustPython/pull/7633/files |
| twitter:image | https://avatars.githubusercontent.com/u/131537123?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/131537123?s=400&v=4 |
| og:image:alt | Summary Formatting a float with a large precision aborts the interpreter instead of raising a Python exception. CPython returns a clean string on the same input. # Before $ ./rustpython -c "pr... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width