Skip to content

Commit

Permalink
Handle type mismatch with invalid handle values (#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Apr 4, 2022
1 parent ea1a991 commit d06738c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
9 changes: 7 additions & 2 deletions crates/libs/bindgen/src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ pub fn gen_win_handle(def: &TypeDef, gen: &Gen) -> TokenStream {

if !invalid.is_empty() {
let invalid = invalid.iter().map(|value| {
let value = Literal::i64_unsuffixed(*value);
quote! { self.0 == #value }
let literal = Literal::i64_unsuffixed(*value);

if *value < 0 && underlying_type.is_unsigned() {
quote! { self.0 == #literal as _ }
} else {
quote! { self.0 == #literal }
}
});
quote! {
impl #ident {
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/metadata/src/reader/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ impl Type {
matches!(self, Self::ConstPtr(_) | Self::MutPtr(_))
}

pub fn is_unsigned(&self) -> bool {
matches!(self, Self::U8 | Self::U16 | Self::U32 | Self::U64 | Self::USize)
}

pub fn is_void(&self) -> bool {
match self {
// TODO: do we care about void behind pointers?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3321,7 +3321,7 @@ pub const GenericTelephonyServiceClassID_UUID16: u32 = 4612u32;
pub struct HANDLE_SDP_TYPE(pub u64);
impl HANDLE_SDP_TYPE {
pub fn is_invalid(&self) -> bool {
self.0 == -1 || self.0 == 0
self.0 == -1 as _ || self.0 == 0
}
}
impl ::core::default::Default for HANDLE_SDP_TYPE {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7947,7 +7947,7 @@ impl ::core::default::Default for MSIFILEHASHINFO {
pub struct MSIHANDLE(pub u32);
impl MSIHANDLE {
pub fn is_invalid(&self) -> bool {
self.0 == -1 || self.0 == 0
self.0 == -1 as _ || self.0 == 0
}
}
impl ::core::default::Default for MSIHANDLE {
Expand Down
2 changes: 2 additions & 0 deletions crates/tests/handles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ features = [
"Win32_Foundation",
"Win32_Graphics_Gdi",
"Win32_System_Registry",
"Win32_Devices_Bluetooth",
"Win32_System_ApplicationInstallationAndServicing",
]

[dependencies.windows-sys]
Expand Down
13 changes: 12 additions & 1 deletion crates/tests/handles/tests/win.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use windows::{Win32::Foundation::*, Win32::Graphics::Gdi::*, Win32::System::Registry::*};
use windows::{Win32::Devices::Bluetooth::*, Win32::Foundation::*, Win32::Graphics::Gdi::*, Win32::System::ApplicationInstallationAndServicing::*, Win32::System::Registry::*};

#[test]
fn handle() {
Expand Down Expand Up @@ -55,3 +55,14 @@ fn const_pattern() {
_ => assert!(false),
}
}

#[test]
fn unsigned_negative_invalid() {
assert!(MSIHANDLE(u32::MAX).is_invalid());
assert!(MSIHANDLE(0).is_invalid());
assert!(!MSIHANDLE(1).is_invalid());

assert!(HANDLE_SDP_TYPE(u64::MAX).is_invalid());
assert!(HANDLE_SDP_TYPE(0).is_invalid());
assert!(!HANDLE_SDP_TYPE(1).is_invalid());
}

0 comments on commit d06738c

Please sign in to comment.