Sitelet https://github.com/starkware-libs/cairo/pull/10035
Skip to content

fix(formatter): avoid usize underflow on over-wide comment prefixes - #10035

Merged
orizi merged 1 commit into
mainfrom
orizi/06-04-fix_formatter_avoid_usize_underflow_on_over-wide_comment_prefixes
Jun 4, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-04-fix_formatter_avoid_usize_underflow_on_over-wide_comment_prefixes

Conversation

@orizi

@orizi orizi commented Jun 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes an integer underflow (panic) in format_leading_comment when computing max_comment_width. The subtraction of cur_indent, n_slashes, n_exclamations, and n_leading_spaces from max_line_width is now done with saturating_sub to prevent wrapping/panicking when the prefix length exceeds max_line_width.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

When a comment line has a prefix (indentation + slashes + exclamations + leading spaces) whose total length exceeds max_line_width, the plain arithmetic subtraction would underflow in debug builds (panic) or wrap in release builds, producing an enormous max_comment_width value and incorrect formatting behavior. This is a real scenario with long section-divider comments made of many slashes.


What was the behavior or documentation before?

A comment like:

//////////////////////////////////////////////////////////////////////////////////////////////////////////// section divider
fn f() {}

would cause a panic (debug) or silent integer wrap (release) during formatting due to the underflow when computing the available comment width.


What is the behavior or documentation after?

max_comment_width saturates to 0 instead of underflowing, allowing the formatter to handle over-long comment prefixes gracefully. The comment is wrapped correctly:

//////////////////////////////////////////////////////////////////////////////////////////////////////////// section
//////////////////////////////////////////////////////////////////////////////////////////////////////////// divider
fn f() {}

A new test case (comment_overflow.cairo) is added to cover this scenario.


Related issue or discussion (if any)

N/A


Additional context

The fix is minimal and surgical — only the four subtractions in format_leading_comment are changed to use saturating_sub.

  format_leading_comment computed max_comment_width as
  `max_line_width - cur_indent - n_slashes - n_exclamations - n_leading_spaces`
  in usize. When the prefix exceeds max_line_width (a deeply-indented or long
  divider comment), this underflows: debug panics, release wraps. Use
  saturating_sub so the width floors at 0 and the comment is left unwrapped.

  Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review June 4, 2026 08:17
@cursor

cursor Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Small, localized change in comment wrapping with a regression test; no auth, I/O, or API surface impact.

Overview
Fixes a panic/wrap in comment formatting when a leading comment’s prefix (indent + //… + spaces) is longer than the configured max line width. In format_leading_comment, computing max_comment_width now uses saturating_sub instead of plain subtraction so the budget cannot underflow.

Adds a formatter test (comment_overflow.cairo) for very long slash-style section dividers: the formatter wraps the comment text onto a second line with the same prefix instead of crashing.

Reviewed by Cursor Bugbot for commit 81c4dd6. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

@eytan-starkware reviewed 4 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jun 4, 2026
Merged via the queue into main with commit 5164bcb Jun 4, 2026
54 checks passed
@orizi
orizi deleted the orizi/06-04-fix_formatter_avoid_usize_underflow_on_over-wide_comment_prefixes branch June 6, 2026 16:23
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