Skip to content

Commit 1158a00

Browse files
committed
fix(linter): prevent integer underflow in count_comment_lines for JSX comments
When processing JSX inline comments like `{/* comment */}` with the eslint/max-lines rule using skipComments: true, the count_comment_lines function would calculate start_line > end_line, causing integer underflow. This resulted in: - Debug mode: panic with "attempt to subtract with overflow" - Release mode: wrapping to usize::MAX, incorrect line count Fix by using saturating_sub which: - Prevents panic in all build modes - Returns 0 for inline comments (logically correct) - Maintains correct behavior for multi-line comments Added test case to prevent regression.
1 parent d65fd52 commit 1158a00

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

crates/oxc_linter/src/utils/comment.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@ pub fn count_comment_lines(comment: &Comment, source_text: &str) -> usize {
2323
if line_has_just_comment(comment_end_line, "*/") {
2424
end_line += 1;
2525
}
26-
end_line - start_line
26+
end_line.saturating_sub(start_line)
27+
}
28+
}
29+
30+
#[cfg(test)]
31+
mod test {
32+
use super::*;
33+
use oxc_allocator::Allocator;
34+
use oxc_parser::Parser;
35+
use oxc_semantic::SemanticBuilder;
36+
use oxc_span::SourceType;
37+
38+
#[test]
39+
fn test_jsx_comment_should_panic() {
40+
let source = r#"export function Component() {
41+
return (
42+
<div>
43+
{/* hello */}
44+
<span>content</span>
45+
</div>
46+
);
47+
}"#;
48+
49+
let allocator = Allocator::default();
50+
let source_type = SourceType::tsx();
51+
let ret = Parser::new(&allocator, source, source_type).parse();
52+
let semantic = SemanticBuilder::new().build(&ret.program).semantic;
53+
54+
// This should panic with integer overflow
55+
for comment in semantic.comments() {
56+
let _lines = count_comment_lines(comment, source);
57+
}
2758
}
2859
}

0 commit comments

Comments
 (0)