Sitelet https://github.com/starkware-libs/cairo/commit/81c4dd6003cecc900f5d68c7ee744515c5c846e7
Skip to content

Commit 81c4dd6

Browse files
committed
fix(formatter): avoid usize underflow on over-wide comment prefixes
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>
1 parent 0ef1296 commit 81c4dd6

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎crates/cairo-lang-formatter/src/formatter_impl.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,13 @@ fn format_leading_comment(content: &str, cur_indent: usize, max_line_width: usiz
794794
let mut last_line_broken = false;
795795
for line in content.lines() {
796796
let orig_comment_line = CommentLine::from_string(line.to_string());
797+
// Saturate: the prefix (indent + slashes + exclamations + leading spaces) can exceed
798+
// `max_line_width` for a deeply-indented or many-slash comment, which would underflow.
797799
let max_comment_width = max_line_width
798-
- cur_indent
799-
- orig_comment_line.n_slashes
800-
- orig_comment_line.n_exclamations
801-
- orig_comment_line.n_leading_spaces;
800+
.saturating_sub(cur_indent)
801+
.saturating_sub(orig_comment_line.n_slashes)
802+
.saturating_sub(orig_comment_line.n_exclamations)
803+
.saturating_sub(orig_comment_line.n_leading_spaces);
802804
// The current line is initialized with the previous line only if it was broken (to avoid
803805
// merging user separated lines).
804806
let mut current_line = if last_line_broken

‎crates/cairo-lang-formatter/src/test.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ use crate::{FormatterConfig, get_formatted_file};
9898
false,
9999
false
100100
)]
101+
#[test_case(
102+
"test_data/cairo_files/comment_overflow.cairo",
103+
"test_data/expected_results/comment_overflow.cairo",
104+
false,
105+
false,
106+
false,
107+
false,
108+
false
109+
)]
101110
#[test_case(
102111
"test_data/cairo_files/use_merge.cairo",
103112
"test_data/expected_results/use_merge.cairo",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////////////////// section divider
2+
fn f() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////////////////// section
2+
//////////////////////////////////////////////////////////////////////////////////////////////////////////// divider
3+
fn f() {}

0 commit comments

Comments
 (0)