Skip to content

Commit b2d59a2

Browse files
refactor(parser): improve safety of char to bytes conversions (#13193)
`to_bytes` converts a `char` to a series of `N` bytes. Add an assertion to make sure that the character *is* actually `N` bytes long. Also, const assert the value of `LOSSY_REPLACEMENT_CHAR_FIRST_BYTE`, rather than just a comment stating what we expect it to be.
1 parent 4f8506e commit b2d59a2

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

crates/oxc_parser/src/lexer/string.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ use super::{
1111

1212
/// Convert `char` to UTF-8 bytes array.
1313
const fn to_bytes<const N: usize>(ch: char) -> [u8; N] {
14+
assert!(ch.len_utf8() == N);
1415
let mut bytes = [0u8; N];
1516
ch.encode_utf8(&mut bytes);
1617
bytes
1718
}
1819

1920
/// Lossy replacement character (U+FFFD) as UTF-8 bytes.
2021
const LOSSY_REPLACEMENT_CHAR_BYTES: [u8; 3] = to_bytes('\u{FFFD}');
21-
const LOSSY_REPLACEMENT_CHAR_FIRST_BYTE: u8 = LOSSY_REPLACEMENT_CHAR_BYTES[0]; // 0xEF
22+
const LOSSY_REPLACEMENT_CHAR_FIRST_BYTE: u8 = LOSSY_REPLACEMENT_CHAR_BYTES[0];
23+
const _: () = assert!(LOSSY_REPLACEMENT_CHAR_FIRST_BYTE == 0xEF);
2224

2325
const MIN_ESCAPED_STR_LEN: usize = 16;
2426

crates/oxc_parser/src/lexer/template.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const MIN_ESCAPED_TEMPLATE_LIT_LEN: usize = 16;
1313

1414
/// Convert `char` to UTF-8 bytes array.
1515
const fn to_bytes<const N: usize>(ch: char) -> [u8; N] {
16+
assert!(ch.len_utf8() == N);
1617
let mut bytes = [0u8; N];
1718
ch.encode_utf8(&mut bytes);
1819
bytes

0 commit comments

Comments
 (0)