You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: FindVolumeHandle::is_invalid() does not check for a value of -1 #1630
FindVolumeHandle::is_invalid() only considers the value 0 to represent an invalid handle which leads to incorrect branches being followed if a function returns a handle value of -1 as its invalid handle code.
use windows::Win32::Storage::FileSystem::{FindFirstVolumeW,FindVolumeClose};fnmain(){unsafe{// will return `-1` due to empty output buffer.let handle = FindFirstVolumeW(&mut[]);if !handle.is_invalid(){// heap corruption due to handle being `-1`FindVolumeClose(handle);}}}
Crate manifest
[package]
name = "find_volume_bug"version = "0.1.0"edition = "2021"
[target.'cfg(windows)'.dependencies]
windows = { version = "0.34.0", features = ["Win32_Storage_FileSystem", "Win32_Foundation"] }
Expected behavior
Body of if block goes unentered because the handle is invalid and the program exits gracefully.
Actual behavior
Body of if block gets entered because the is_invalid fails to catch the value -1 and the program exits due to heap corruption.
I believe this is related to microsoft/win32metadata#25. That is, this API has metadata describing a specialized handle return type. So because it's not a windows::Win32::Foundation::HANDLE, the -1 check doesn't happen. (Some APIs use -1, null, 666, etc. as invalid handle values.) I think we need to consider bumping up priority of this issue on the metadata side.
Unfortunately, there's no metadata describing what constitutes an invalid handle. Right now is_invalid for most handles just assumes that 0 is invalid:
Activity
riverar commentedon Mar 24, 2022
Thanks @IanE9.
I believe this is related to microsoft/win32metadata#25. That is, this API has metadata describing a specialized handle return type. So because it's not a
windows::Win32::Foundation::HANDLE, the-1check doesn't happen. (Some APIs use-1,null,666, etc. as invalid handle values.) I think we need to consider bumping up priority of this issue on the metadata side.Will double-check with Kenny Kerr (@kennykerr) before transferring.
kennykerr commentedon Mar 24, 2022
Unfortunately, there's no metadata describing what constitutes an invalid handle. Right now
is_invalidfor most handles just assumes that0is invalid:windows-rs/crates/libs/bindgen/src/handles.rs
Lines 31 to 33 in c31774a
Unless the metadata is added, we could just assume that
-1may also be an invalid value asHANDLEcurrently does:windows-rs/crates/libs/bindgen/src/replacements/handle.rs
Lines 8 to 10 in c31774a
Or we could just get out of the business of providing an
is_invalidfunction altogether and leave that up to the caller to figure out.There aren't all that many handle types, so perhaps adding the metadata would be the best route.