Skip to content

Commit

Permalink
fix potential double close on windows (#317)
Browse files Browse the repository at this point in the history
If `.metadata` returns an `err`, then both `file` and `sys_file` will be
dropped, closing the handle twice. Use ManuallyDrop to pre-declare the
intention to not close `sys_file`.

This is more of a bug report than a pull request. I haven't tested the
code, and I don't intend to, feel free to take over this!
  • Loading branch information
matklad authored Oct 31, 2024
1 parent c9708e8 commit 90c9c3e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions monoio/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,12 @@ pub async fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
let file = File::open(path).await?;

#[cfg(windows)]
let sys_file = unsafe { std::fs::File::from_raw_handle(file.as_raw_handle()) };
#[cfg(windows)]
let size = sys_file.metadata()?.len() as usize;
#[cfg(windows)]
let _ = sys_file.into_raw_handle();
let size = {
let sys_file = std::mem::ManuallyDrop::new(unsafe {
std::fs::File::from_raw_handle(file.as_raw_handle())
});
sys_file.metadata()?.len() as usize
};

#[cfg(unix)]
let size = file.metadata().await?.len() as usize;
Expand Down

0 comments on commit 90c9c3e

Please sign in to comment.