From 90c9c3e7bd1b3166e29bf198aad12fdc0fb63dbd Mon Sep 17 00:00:00 2001 From: Alex Kladov Date: Thu, 31 Oct 2024 03:08:33 +0000 Subject: [PATCH] fix potential double close on windows (#317) 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! --- monoio/src/fs/mod.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/monoio/src/fs/mod.rs b/monoio/src/fs/mod.rs index 7d5fd621..a5b586f2 100644 --- a/monoio/src/fs/mod.rs +++ b/monoio/src/fs/mod.rs @@ -166,11 +166,12 @@ pub async fn read>(path: P) -> io::Result> { 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;