From a33f35a443dd1c28349bd2b3ba570c85694864c1 Mon Sep 17 00:00:00 2001 From: longjin Date: Sun, 5 Nov 2023 15:15:15 +0000 Subject: [PATCH] fix potential memory problem --- kernel/src/filesystem/vfs/file.rs | 10 ++++------ user/libs/libc/src/Makefile | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/kernel/src/filesystem/vfs/file.rs b/kernel/src/filesystem/vfs/file.rs index dbe4c59bb..3385f43c5 100644 --- a/kernel/src/filesystem/vfs/file.rs +++ b/kernel/src/filesystem/vfs/file.rs @@ -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; diff --git a/user/libs/libc/src/Makefile b/user/libs/libc/src/Makefile index e2d8ec834..420c68f0f 100644 --- a/user/libs/libc/src/Makefile +++ b/user/libs/libc/src/Makefile @@ -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