Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(codegen): prevent arithmetic overflow calculating quote for `Stri…
…ngLiteral`s (#10102)

Code for calculating the optimum quote character to use for `StringLiteral`s could in theory produce arithmetic overflow (panic in debug mode, malfunction in release mode) if the string is very long and contains a very large number of quote characters e.g. `"''''''''''''"` (but much much longer).

Fix that by using `i64` instead of `i32` for the counter variables.
  • Loading branch information
overlookmotel committed Mar 29, 2025
commit c903e427759532df38227b2764e2270a5a1f4c10
9 changes: 5 additions & 4 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,14 @@ impl<'a> Codegen<'a> {
self.add_source_mapping(s.span);

let quote = if self.options.minify {
let mut single_cost: i32 = 0;
let mut double_cost: i32 = 0;
let mut backtick_cost: i32 = 0;
// String length is max `u32::MAX`, so use `i64` to make overflow impossible
let mut single_cost: i64 = 0;
let mut double_cost: i64 = 0;
let mut backtick_cost: i64 = 0;
let mut bytes = s.value.as_bytes().iter().peekable();
while let Some(b) = bytes.next() {
match b {
b'\n' if self.options.minify => backtick_cost = backtick_cost.saturating_sub(1),
b'\n' => backtick_cost -= 1,
b'\'' => single_cost += 1,
b'"' => double_cost += 1,
b'`' => backtick_cost += 1,
Expand Down