Align dirent headers

This commit is contained in:
Jeremy Soller
2026-05-04 12:28:53 -06:00
parent 271374cd4d
commit d3c6430dc0
+4 -1
View File
@@ -180,12 +180,15 @@ impl<'a, B: Buffer<'a>> DirentBuf<B> {
}
pub fn entry(&mut self, entry: DirEntry<'_>) -> Result<()> {
let name16 = u16::try_from(entry.name.len()).map_err(|_| Error::new(EINVAL))?;
let record_align = align_of::<*const DirentHeader>();
let record_len = self
.header_size
.checked_add(name16)
// XXX: NUL byte. Unfortunately this is probably the only performant way to be
// compatible with C.
.and_then(|l| l.checked_add(1))
// Align length so next header is aligned
.and_then(|l| l.checked_next_multiple_of(record_align as u16))
.ok_or(Error::new(ENAMETOOLONG))?;
let [this, remaining] = core::mem::replace(&mut self.buffer, B::empty())
@@ -215,7 +218,7 @@ impl<'a, B: Buffer<'a>> DirentBuf<B> {
})?;
this_header_extra.zero_out()?;
this_name.copy_from_slice_exact(entry.name.as_bytes())?;
this_name_nul.copy_from_slice_exact(&[0])?;
this_name_nul.zero_out()?;
self.written += usize::from(record_len);
self.buffer = remaining;