Skip to content

Reject format spec with width above i32::MAX#7707

Merged
youknowone merged 1 commit intoRustPython:mainfrom
changjoon-park:fix-format-huge-width
Apr 28, 2026
Merged

Reject format spec with width above i32::MAX#7707
youknowone merged 1 commit intoRustPython:mainfrom
changjoon-park:fix-format-huge-width

Conversation

@changjoon-park
Copy link
Copy Markdown
Contributor

@changjoon-park changjoon-park commented Apr 27, 2026

Background

CPython rejects format-spec widths that exceed Py_ssize_t::MAX with ValueError: Too many decimal digits in format string. RustPython's FormatSpec::_parse only capped precision (via parse_precision); width digits were accepted up to usize::MAX, so values like sys.maxsize + 1 silently produced an effectively-ignored width.

Repro

import sys
fs = '{}f'.format(sys.maxsize + 1)  # '9223372036854775808f'

format(2.34, fs)
# CPython 3.14:    ValueError: Too many decimal digits in format string
# RustPython:      '2.340000'        (silently dropped huge width, before this PR)

Fix

After parse_number returns the width in FormatSpec::_parse, reject any value above i32::MAX with FormatSpecError::DecimalDigitsTooMany. This matches the cap precision already uses and produces the byte-identical CPython "Too many decimal digits in format string" ValueError.

Tests unmasked

  • test_str.StrTest.test_format_huge_width

Verification

  • CPython 3.14.4 byte-identical wording for the huge-width case
  • Precision path unchanged (test_format_huge_precision still passes with its existing "Precision too big" message)
  • Normal widths (e.g. format(2.34, '100.3f')) still pad correctly
  • No regressions across 7 modules: test_str, test_format, test_unicode_identifiers, test_fstring, test_int, test_float, test_decimal

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved validation for format specifications to properly reject overly large width values with appropriate error handling during parsing.

CPython rejects format-spec widths that exceed Py_ssize_t::MAX with
ValueError: Too many decimal digits in format string. RustPython's
FormatSpec::_parse only capped precision (via parse_precision); width
was accepted up to usize::MAX, so values like sys.maxsize + 1 silently
produced an effectively-ignored width.

Reject any width above i32::MAX with FormatSpecError::DecimalDigitsTooMany,
matching the existing precision cap and producing the byte-identical
ValueError wording.

Unmasks test_str.StrTest.test_format_huge_width.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: e15fe342-a776-4f67-b57e-3ffc66832ba9

📥 Commits

Reviewing files that changed from the base of the PR and between 1d42ee5 and 999a211.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_str.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/common/src/format.rs

📝 Walkthrough

Walkthrough

Format-spec parsing now validates parsed width values against i32::MAX to prevent overflow, returning FormatSpecError::DecimalDigitsTooMany when widths exceed the valid range. This early validation prevents invalid values from propagating through subsequent computations.

Changes

Cohort / File(s) Summary
Format spec validation
crates/common/src/format.rs
Added bounds check to validate parsed width against i32::MAX, failing early with DecimalDigitsTooMany error for oversized values.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐰 A width too wide would cause us strife,
So now we check before it takes a life,
i32::MAX sets the bound so tight,
Early validation keeps our parsing right! 🎯

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main change: adding validation to reject format specs where width exceeds i32::MAX, which is the core modification in the format.rs file.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] test: cpython/Lib/test/test_str.py (TODO: 14)
[x] test: cpython/Lib/test/test_fstring.py (TODO: 19)
[x] test: cpython/Lib/test/test_string_literals.py (TODO: 4)

dependencies:

dependent tests: (no tests depend on str)

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳
ty:)

Copy link
Copy Markdown
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@youknowone youknowone merged commit b3d6d2f into RustPython:main Apr 28, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants