Skip to content

Commit cb7f006

Browse files
authored
fix(shmem): close file handle on failed mapping (#55)
1 parent 49499ea commit cb7f006

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/shmem.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,18 @@ fn map_file(file: &File, size: usize) -> Result<NonNull<u8>, Error> {
9595
let mmap = unsafe { MapViewOfFile(mapping, FILE_MAP_ALL_ACCESS, 0, 0, size) };
9696

9797
if mmap.Value.is_null() {
98-
return Err(Error::Mmap(std::io::Error::last_os_error()));
99-
}
98+
let err = Error::Mmap(std::io::Error::last_os_error());
10099

101-
unsafe {
102-
CloseHandle(mapping);
103-
}
100+
unsafe { CloseHandle(mapping) };
104101

105-
Ok(NonNull::new(mmap.Value.cast()).expect("already checked for null"))
102+
Err(err)
103+
} else {
104+
unsafe {
105+
CloseHandle(mapping);
106+
}
107+
108+
Ok(NonNull::new(mmap.Value.cast()).expect("already checked for null"))
109+
}
106110
}
107111

108112
/// Unmaps a previously mapped file view.

0 commit comments

Comments
 (0)