Skip to content

Commit

Permalink
fix potential memory problem
Browse files Browse the repository at this point in the history
  • Loading branch information
fslongjin committed Nov 5, 2023
1 parent 92e9adc commit a33f35a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
10 changes: 4 additions & 6 deletions kernel/src/filesystem/vfs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,11 @@ impl File {
// 根据posix的规定,dirent中的d_name是一个不定长的数组,因此需要unsafe来拷贝数据
unsafe {
let ptr = &mut dirent.d_name as *mut u8;
if self.offset > 0 {
// 将上一次读取到的缓冲区清0,避免这一次的数据携带上一次的脏数据
core::ptr::write_bytes(ptr, 0, self.readdir_subdirs_name[self.offset - 1].len())
}

let buf: &mut [u8] =
::core::slice::from_raw_parts_mut::<'static, u8>(ptr, name_bytes.len());
buf.copy_from_slice(name_bytes);
::core::slice::from_raw_parts_mut::<'static, u8>(ptr, name_bytes.len() + 1);
buf[0..name_bytes.len()].copy_from_slice(name_bytes);
buf[name_bytes.len()] = 0;
}

self.offset += 1;
Expand Down
3 changes: 1 addition & 2 deletions user/libs/libc/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ clean:
libc: $(libc_objs) $(libc_sub_dirs) libc_rust

libc_rust:
rustup default nightly
cargo +nightly build --release --target ./arch/x86_64/x86_64-unknown-none.json
cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json

0 comments on commit a33f35a

Please sign in to comment.