Skip to content

Commit

Permalink
Fixes a bug when FileInfoBuf is dropped
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed Sep 2, 2023
1 parent c443eac commit 864649f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::{EfiStr, Guid, Owned, Status, Time};
use alloc::alloc::{alloc, dealloc, handle_alloc_error};
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use bitflags::bitflags;
use core::alloc::Layout;
use core::borrow::{Borrow, BorrowMut};
use core::fmt::{Display, Formatter};
use core::mem::zeroed;
use core::ops::{Deref, DerefMut};
use core::ptr::{null_mut, read};
use core::slice::from_raw_parts_mut;
use core::slice::{from_raw_parts, from_raw_parts_mut};

/// Represents an `EFI_SIMPLE_FILE_SYSTEM_PROTOCOL`.
#[repr(C)]
Expand Down Expand Up @@ -159,7 +158,7 @@ impl File {
let status = unsafe { (self.get_info)(self, &FileInfo::ID, &mut len, info) };

if status == Status::SUCCESS {
break unsafe { from_raw_parts_mut(info, layout.size()) };
break info;
}

// Check if we need to try again.
Expand All @@ -173,7 +172,10 @@ impl File {
layout = Layout::from_size_align(len, 8).unwrap();
};

Ok(FileInfoBuf(unsafe { Box::from_raw(info) }))
Ok(FileInfoBuf {
buf: info,
len: layout.size(),
})
}

pub fn set_len(&mut self, len: u64) -> Result<(), FileSetLenError> {
Expand Down Expand Up @@ -321,12 +323,21 @@ impl ToOwned for FileInfo {

unsafe { buf.copy_from_nonoverlapping(self.0.as_ptr(), len) };

FileInfoBuf(unsafe { Box::from_raw(from_raw_parts_mut(buf, len)) })
FileInfoBuf { buf, len }
}
}

/// An owned version of [`FileInfo`].
pub struct FileInfoBuf(Box<[u8]>); // We need to make sure the pointer is 8 bytes aligment.
pub struct FileInfoBuf {
buf: *mut u8, // Must be 8 bytes aligment.
len: usize,
}

impl Drop for FileInfoBuf {
fn drop(&mut self) {
unsafe { dealloc(self.buf, Layout::from_size_align(self.len, 8).unwrap()) };
}
}

impl Deref for FileInfoBuf {
type Target = FileInfo;
Expand All @@ -344,13 +355,13 @@ impl DerefMut for FileInfoBuf {

impl Borrow<FileInfo> for FileInfoBuf {
fn borrow(&self) -> &FileInfo {
unsafe { &*(self.0.as_ref() as *const [u8] as *const FileInfo) }
unsafe { &*(from_raw_parts(self.buf, self.len) as *const [u8] as *const FileInfo) }
}
}

impl BorrowMut<FileInfo> for FileInfoBuf {
fn borrow_mut(&mut self) -> &mut FileInfo {
unsafe { &mut *(self.0.as_mut() as *mut [u8] as *mut FileInfo) }
unsafe { &mut *(from_raw_parts_mut(self.buf, self.len) as *mut [u8] as *mut FileInfo) }
}
}

Expand Down

0 comments on commit 864649f

Please sign in to comment.