Skip to content

Commit 5a17750

Browse files
committed
Add comments to explain new unsafe code.
1 parent 0192030 commit 5a17750

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ macro_rules! _cssparser_internal__max_len {
174174
struct Dummy;
175175

176176
// MAX_LENGTH is generated by cssparser__match_ignore_ascii_case__max_len
177-
let mut buffer: [u8; MAX_LENGTH] = unsafe { ::std::mem::uninitialized() };
177+
let mut buffer: [u8; MAX_LENGTH] =
178+
// `buffer` is only used in `_match_ignore_ascii_case__to_lowercase`,
179+
// which initializes with `copy_from_slice` the part of the buffer it uses,
180+
// before it uses it.
181+
unsafe {
182+
::std::mem::uninitialized()
183+
};
178184
let $output = $crate::_match_ignore_ascii_case__to_lowercase(&mut buffer, $input);
179185
}
180186
}
@@ -188,6 +194,8 @@ pub fn _match_ignore_ascii_case__to_lowercase<'a>(buffer: &'a mut [u8], input: &
188194
if let Some(first_uppercase) = input.bytes().position(|byte| matches!(byte, b'A'...b'Z')) {
189195
buffer.copy_from_slice(input.as_bytes());
190196
std::ascii::AsciiExt::make_ascii_lowercase(&mut buffer[first_uppercase..]);
197+
// `buffer` was initialized to a copy of `input` (which is &str so well-formed UTF-8)
198+
// then lowercased (which preserves UTF-8 well-formedness)
191199
unsafe {
192200
Some(::std::str::from_utf8_unchecked(buffer))
193201
}

0 commit comments

Comments
 (0)