Browse Source

Correctly handle possibly misaligned pointers in readdir (#615)

pull/647/head
Marcin Mielniczuk 5 years ago
committed by Dan Gohman
parent
commit
b69758f672
  1. 2
      crates/test-programs/wasi-tests/src/bin/fd_readdir.rs
  2. 4
      crates/wasi-common/src/hostcalls_impl/fs.rs

2
crates/test-programs/wasi-tests/src/bin/fd_readdir.rs

@ -36,7 +36,7 @@ impl<'a> Iterator for ReadDir<'a> {
// Read the data
let dirent_ptr = self.buf.as_ptr() as *const wasi_unstable::Dirent;
let dirent = *dirent_ptr;
let dirent = dirent_ptr.read_unaligned();
let name_ptr = dirent_ptr.offset(1) as *const u8;
// NOTE Linux syscall returns a NULL-terminated name, but WASI doesn't
let namelen = dirent.d_namlen as usize;

4
crates/wasi-common/src/hostcalls_impl/fs.rs

@ -1088,12 +1088,12 @@ impl Dirent {
let sys_dirent = raw.as_mut_ptr() as *mut wasi::__wasi_dirent_t;
unsafe {
*sys_dirent = wasi::__wasi_dirent_t {
sys_dirent.write_unaligned(wasi::__wasi_dirent_t {
d_namlen: namlen.try_into()?,
d_ino: self.ino,
d_next: self.cookie,
d_type: self.ftype.to_wasi(),
};
});
}
let sys_name = unsafe { sys_dirent.offset(1) as *mut u8 };

Loading…
Cancel
Save