From 27a28087764cb577a175514b94ffafe53dd7c747 Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Tue, 21 Jul 2020 19:49:32 +0200 Subject: [PATCH] Fix some clippy warnings and mark additional public functions as unsafe --- Cargo.toml | 2 +- src/lib.rs | 1 + src/result_enumerator.rs | 30 +++++++++++++----------------- src/safearray.rs | 12 +++++++++--- src/variant.rs | 9 +++++++-- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5cd6c4a..f6a508a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wmi" -version = "0.5.0" +version = "0.6.0-a" authors = ["Ohad Ravid "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index 481f0b5..adea7a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,6 +100,7 @@ //! #![allow(non_camel_case_types)] #![allow(non_snake_case)] +#![allow(unused_unsafe)] #![cfg(windows)] pub mod connection; diff --git a/src/result_enumerator.rs b/src/result_enumerator.rs index 241417d..6404e88 100644 --- a/src/result_enumerator.rs +++ b/src/result_enumerator.rs @@ -49,16 +49,15 @@ impl IWbemClassWrapper { (WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY) as i32, ptr::null_mut(), &mut p_names, - )) - }?; + ))?; + - let res = safe_array_to_vec_of_strings(p_names); + let res = safe_array_to_vec_of_strings(p_names); - unsafe { check_hres(SafeArrayDestroy(p_names))?; - } - res + res + } } pub fn get_property(&self, property_name: &str) -> Result { @@ -75,13 +74,14 @@ impl IWbemClassWrapper { ptr::null_mut(), ptr::null_mut(), ); - } + - let property_value = Variant::from_variant(vt_prop)?; + let property_value = Variant::from_variant(vt_prop)?; - unsafe { VariantClear(&mut vt_prop) }; + VariantClear(&mut vt_prop); - Ok(property_value) + Ok(property_value) + } } pub fn path(&self) -> Result { @@ -142,12 +142,8 @@ impl<'a> Iterator for QueryResultEnumerator<'a> { fn next(&mut self) -> Option { let mut pcls_obj = NULL as *mut IWbemClassObject; let mut return_value = 0; - - if self.p_enumerator.is_none() { - return None; - } - - let raw_enumerator_prt = self.p_enumerator.unwrap().as_ptr(); + + let raw_enumerator_prt = self.p_enumerator?.as_ptr(); let res = unsafe { check_hres((*raw_enumerator_prt).Next( @@ -159,7 +155,7 @@ impl<'a> Iterator for QueryResultEnumerator<'a> { }; if let Err(e) = res { - return Some(Err(e.into())); + return Some(Err(e)); } if return_value == 0 { diff --git a/src/safearray.rs b/src/safearray.rs index 712ae9b..1a3f0ce 100644 --- a/src/safearray.rs +++ b/src/safearray.rs @@ -47,7 +47,6 @@ impl SafeArrayAccessor { let mut lower_bound: i32 = 0; let mut upper_bound: i32 = 0; - #[allow(unused_unsafe)] unsafe { check_hres(SafeArrayGetLBound(arr, 1, &mut lower_bound as _))?; check_hres(SafeArrayGetUBound(arr, 1, &mut upper_bound as _))?; @@ -81,7 +80,10 @@ impl Drop for SafeArrayAccessor { } } -pub fn safe_array_to_vec_of_strings(arr: *mut SAFEARRAY) -> Result, WMIError> { +/// # Safety +/// +/// The caller must ensure that the array is valid and contains only strings. +pub unsafe fn safe_array_to_vec_of_strings(arr: *mut SAFEARRAY) -> Result, WMIError> { let items = safe_array_to_vec(arr, VT_BSTR)?; let string_items = items @@ -95,7 +97,11 @@ pub fn safe_array_to_vec_of_strings(arr: *mut SAFEARRAY) -> Result, Ok(string_items) } -pub fn safe_array_to_vec(arr: *mut SAFEARRAY, item_type: u32) -> Result, WMIError> { + +/// # Safety +/// +/// The caller must ensure that the array is valid. +pub unsafe fn safe_array_to_vec(arr: *mut SAFEARRAY, item_type: u32) -> Result, WMIError> { let mut items = vec![]; match item_type { diff --git a/src/variant.rs b/src/variant.rs index 73d830a..66f16c5 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -35,7 +35,12 @@ pub enum Variant { } impl Variant { - pub fn from_variant(vt: VARIANT) -> Result { + /// Create a `Variant` instance from a raw `VARIANT`. + /// + /// # Safety + /// + /// This function is unsafe as it is the caller's responsibility to ensure that the VARIANT is correctly initialized. + pub unsafe fn from_variant(vt: VARIANT) -> Result { let variant_type: VARTYPE = unsafe { vt.n1.n2().vt }; // variant_type has two 'forms': @@ -46,7 +51,7 @@ impl Variant { let item_type = variant_type as u32 & VT_TYPEMASK; - return Ok(Variant::Array(safe_array_to_vec(*array, item_type as u32)?)); + return Ok(Variant::Array(unsafe { safe_array_to_vec(*array, item_type as u32)? })); } // See https://msdn.microsoft.com/en-us/library/cc237865.aspx for more info.