Skip to content

Commit

Permalink
fix(minifier): fix panic in peephole_minimize_conditions (#8242)
Browse files Browse the repository at this point in the history
https://github.com/oxc-project/monitor-oxc caught some crashes.

Make things safer by returning falling back to true.
  • Loading branch information
Boshen committed Jan 4, 2025
1 parent ad9a0a9 commit d2f8eaa
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,6 @@ impl<'a> PeepholeMinimizeConditions {
}
}

let in_boolean_context = Self::is_in_boolean_context(ctx);

// `a ? false : true` -> `!a`
// `a ? true : false` -> `!!a`
if let (
Expand All @@ -377,7 +375,7 @@ impl<'a> PeepholeMinimizeConditions {
(true, false) => {
let ident = ctx.ast.move_expression(&mut expr.test);

if in_boolean_context {
if Self::is_in_boolean_context(ctx) {
return Some(ident);
}
return Some(ctx.ast.expression_unary(
Expand All @@ -399,7 +397,7 @@ impl<'a> PeepholeMinimizeConditions {
let ident = ctx.ast.move_expression(&mut expr.test);
return Some(ctx.ast.expression_logical(
expr.span,
if in_boolean_context {
if Self::is_in_boolean_context(ctx) {
ident
} else {
ctx.ast.expression_unary(
Expand Down Expand Up @@ -438,7 +436,7 @@ impl<'a> PeepholeMinimizeConditions {
let ident = ctx.ast.move_expression(&mut expr.test);
return Some(ctx.ast.expression_logical(
expr.span,
if in_boolean_context {
if Self::is_in_boolean_context(ctx) {
ident
} else {
ctx.ast.expression_unary(
Expand Down Expand Up @@ -467,7 +465,9 @@ impl<'a> PeepholeMinimizeConditions {
| Ancestor::WhileStatementTest(_)
| Ancestor::ForStatementTest(_)
| Ancestor::DoWhileStatementTest(_)
| Ancestor::ExpressionStatementExpression(_) => return true,
| Ancestor::ExpressionStatementExpression(_)
| Ancestor::SequenceExpressionExpressions(_)
| Ancestor::ProgramBody(_) => return true,
Ancestor::CallExpressionArguments(_)
| Ancestor::AssignmentPatternRight(_)
| Ancestor::BindingRestElementArgument(_)
Expand All @@ -482,10 +482,7 @@ impl<'a> PeepholeMinimizeConditions {
_ => continue,
}
}
#[cfg(debug_assertions)]
unreachable!();
#[cfg(not(debug_assertions))]
false
true
}

fn try_minimize_binary(
Expand Down

0 comments on commit d2f8eaa

Please sign in to comment.