Skip to content

Commit

Permalink
fix(codegen): fix arithmetic overflow printing unspanned nodes (#7292)
Browse files Browse the repository at this point in the history
Similar to #7289. Check if `span.end` is 0 before doing `span.end - 1`, to prevent arithmetic overflow.

Also changed all checks to `span.end > 0`, just for consistency.
  • Loading branch information
overlookmotel authored and Dunqing committed Nov 18, 2024
1 parent db5dfe7 commit 8570728
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl<'a> Gen for Function<'a> {
impl<'a> Gen for FunctionBody<'a> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
let span_end = self.span.end;
let comments_at_end = if !p.options.minify && span_end != 0 {
let comments_at_end = if !p.options.minify && span_end > 0 {
p.get_statement_comments(span_end - 1)
} else {
None
Expand Down Expand Up @@ -1985,7 +1985,7 @@ impl<'a> GenExpr for ImportExpression<'a> {
}
if has_comment {
// Handle `/* comment */);`
if !p.print_expr_comments(self.span.end - 1) {
if self.span.end > 0 && !p.print_expr_comments(self.span.end - 1) {
p.print_soft_newline();
}
p.dedent();
Expand Down Expand Up @@ -2067,13 +2067,13 @@ impl<'a> GenExpr for NewExpression<'a> {
p.print_str("new ");
self.callee.print_expr(p, Precedence::New, Context::FORBID_CALL);
p.print_ascii_byte(b'(');
let has_comment = (!self.span.is_unspanned() && p.has_comment(self.span.end - 1))
let has_comment = (self.span.end > 0 && p.has_comment(self.span.end - 1))
|| self.arguments.iter().any(|item| p.has_comment(item.span().start));
if has_comment {
p.indent();
p.print_list_with_comments(&self.arguments, ctx);
// Handle `/* comment */);`
if !p.print_expr_comments(self.span.end - 1) {
if self.span.end > 0 && !p.print_expr_comments(self.span.end - 1) {
p.print_soft_newline();
}
p.dedent();
Expand Down

0 comments on commit 8570728

Please sign in to comment.