Skip to content

Commit

Permalink
fix(endianess): fix potential endianess issue when transmuting an array
Browse files Browse the repository at this point in the history
Transmuting an array of u8 to u16 theoretically wouldn't work on big-endian systems, since FAT is little-endian
  • Loading branch information
Oakchris1955 committed Aug 3, 2024
1 parent fa487ed commit 54962a1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,18 @@ struct LFNEntry {

impl LFNEntry {
fn get_byte_slice(&self) -> [u16; 13] {
let mut slice = [0_u8; 13 * 2];
let mut slice = [0_u8; 13 * mem::size_of::<u16>()];

slice[..10].copy_from_slice(&self.first_chars);
slice[10..22].copy_from_slice(&self.mid_chars);
slice[22..].copy_from_slice(&self.last_chars);

// this is safe since u8 is half the size of u16 and the len of the src slice is even
unsafe { slice.align_to().1.try_into().unwrap() }
let mut out_slice = [0_u16; 13];
for (i, chunk) in slice.chunks(mem::size_of::<u16>()).enumerate() {
out_slice[i] = u16::from_le_bytes(chunk.try_into().unwrap());
}

out_slice
}

#[inline]
Expand Down

0 comments on commit 54962a1

Please sign in to comment.