Skip to content

Commit 3279150

Browse files
authored
fix: resolve clippy lints in win-api-wrappers for strict enforcement (#1563)
1 parent e822378 commit 3279150

File tree

9 files changed

+27
-18
lines changed

9 files changed

+27
-18
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ obj/
77
/output/
88
/config/
99
*.DotSettings
10-
.angular
10+
.angular

crates/win-api-wrappers/src/fs.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ pub fn get_system32_path() -> anyhow::Result<String> {
2525
// SAFETY:
2626
// - `buffer.as_mut_ptr()` gives a valid writable pointer for MAX_PATH u16s.
2727
// - `GetSystemDirectoryW` expects a valid mutable wide string buffer.
28-
let len = unsafe { GetSystemDirectoryW(Some(std::slice::from_raw_parts_mut(buffer.as_mut_ptr(), buffer.len()))) };
28+
let len = {
29+
// SAFETY: We construct a valid slice from the buffer.
30+
let slice = unsafe { std::slice::from_raw_parts_mut(buffer.as_mut_ptr(), buffer.len()) };
31+
// SAFETY: slice is a valid mutable wide string buffer.
32+
unsafe { GetSystemDirectoryW(Some(slice)) }
33+
};
2934

3035
if len == 0 {
3136
anyhow::bail!("GetSystemDirectoryW failed");

crates/win-api-wrappers/src/identity/account.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ pub fn enumerate_account_rights(sid: &Sid) -> anyhow::Result<Vec<U16CString>> {
376376

377377
if open_policy_status.is_err() {
378378
// Convert NTSTATUS to a Win32 error code and return as an error
379+
// SAFETY: LsaNtStatusToWinError is always safe to call with any NTSTATUS value.
379380
let error_code = unsafe { Identity::LsaNtStatusToWinError(open_policy_status) };
380381
let error_code = WIN32_ERROR(error_code);
381382

@@ -387,12 +388,14 @@ pub fn enumerate_account_rights(sid: &Sid) -> anyhow::Result<Vec<U16CString>> {
387388
let mut rights = ScopeGuard::new(ptr::null_mut::<Identity::LSA_UNICODE_STRING>(), |ptr| {
388389
if !ptr.is_null() {
389390
// FIXME: maybe we should log the error here.
391+
// SAFETY: ptr is a valid pointer returned by LsaEnumerateAccountRights.
390392
let _ = unsafe { Identity::LsaFreeMemory(Some(ptr as *const std::ffi::c_void)) };
391393
}
392394
});
393395

394396
let mut rights_count: u32 = 0;
395397

398+
// SAFETY: We pass valid pointers and policy_handle was obtained from LsaOpenPolicy.
396399
let enum_status = unsafe {
397400
Identity::LsaEnumerateAccountRights(
398401
*policy_handle.as_ref(),
@@ -407,6 +410,7 @@ pub fn enumerate_account_rights(sid: &Sid) -> anyhow::Result<Vec<U16CString>> {
407410
Vec::new()
408411
} else if enum_status.is_err() {
409412
// Convert NTSTATUS to a Win32 error code and return as an error
413+
// SAFETY: LsaNtStatusToWinError is always safe to call with any NTSTATUS value.
410414
let error_code = unsafe { Identity::LsaNtStatusToWinError(enum_status) };
411415
let error_code = WIN32_ERROR(error_code);
412416

crates/win-api-wrappers/src/netmgmt.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ pub fn get_local_group_members(group_name: &U16CStr) -> anyhow::Result<Vec<Sid>>
101101
// - `NetLocalGroupGetMembers` sets `group_members` to a valid pointer on success.
102102
// - `group_members` must be freed by `NetApiBufferFree`.
103103
// - For level = 0, bufptr will be set to a pointer to a LOCALGROUP_MEMBERS_INFO_0.
104+
// - NetLocalGroupGetMembers returns a properly aligned pointer for LOCALGROUP_MEMBERS_INFO_0.
105+
#[expect(
106+
clippy::cast_ptr_alignment,
107+
reason = "NetLocalGroupGetMembers guarantees proper alignment"
108+
)]
104109
let group_members = unsafe { NetmgmtMemory::from_raw(group_members.cast::<LOCALGROUP_MEMBERS_INFO_0>()) };
105110

106111
// SAFETY:

crates/win-api-wrappers/src/process.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,10 @@ impl Drop for ProcessEnvironment {
671671
// SAFETY: `ProcessEnvironment` is a private enum, and we ensured that `block` will only
672672
// ever hold pointers returned by `CreateEnvironmentBlock` in the current module.
673673
unsafe {
674-
if !block.is_null() {
675-
if let Err(error) = DestroyEnvironmentBlock(*block) {
676-
warn!(%error, "Failed to destroy environment block");
677-
}
674+
if !block.is_null()
675+
&& let Err(error) = DestroyEnvironmentBlock(*block)
676+
{
677+
warn!(%error, "Failed to destroy environment block");
678678
}
679679
};
680680
}

crates/win-api-wrappers/src/rpc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub struct RpcServerInterfacePointer {
118118
pub raw: &'static RPC_SERVER_INTERFACE,
119119
}
120120

121+
// SAFETY: RpcServerInterfacePointer only contains a static reference, which is safe to send across threads.
121122
unsafe impl Send for RpcServerInterfacePointer {}
122123

123124
impl RpcServerInterfacePointer {

crates/win-api-wrappers/src/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Token {
9595
let sqos = SECURITY_QUALITY_OF_SERVICE {
9696
Length: u32size_of::<SECURITY_QUALITY_OF_SERVICE>(),
9797
ImpersonationLevel: SecurityImpersonation,
98-
ContextTrackingMode: Security::SECURITY_DYNAMIC_TRACKING as u8,
98+
ContextTrackingMode: u8::from(Security::SECURITY_DYNAMIC_TRACKING),
9999
EffectiveOnly: false,
100100
};
101101

crates/win-api-wrappers/src/utils.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,25 +258,19 @@ impl CommandLine {
258258
if chars.peek().is_some() {
259259
backslashes += 1
260260
} else {
261-
std::iter::repeat('\\')
262-
.take(backslashes * 2)
263-
.for_each(|x| command_line.push(x));
261+
std::iter::repeat_n('\\', backslashes * 2).for_each(|x| command_line.push(x));
264262

265263
backslashes = 0;
266264
}
267265
}
268266
'"' => {
269-
std::iter::repeat('\\')
270-
.take(backslashes * 2 + 1)
271-
.for_each(|x| command_line.push(x));
267+
std::iter::repeat_n('\\', backslashes * 2 + 1).for_each(|x| command_line.push(x));
272268

273269
command_line.push('"');
274270
backslashes = 0;
275271
}
276272
x => {
277-
std::iter::repeat('\\')
278-
.take(backslashes)
279-
.for_each(|x| command_line.push(x));
273+
std::iter::repeat_n('\\', backslashes).for_each(|x| command_line.push(x));
280274

281275
command_line.push(x);
282276
backslashes = 0;

crates/win-api-wrappers/src/wts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ pub fn send_message_to_session(
217217
Some(WTS_CURRENT_SERVER_HANDLE),
218218
session_id,
219219
title.as_pcwstr(),
220-
(title.0.unwrap().len() * size_of_val(&0u16)).try_into()?,
220+
(title.0.expect("title buffer is always Some").len() * size_of_val(&0u16)).try_into()?,
221221
message.as_pcwstr(),
222-
(message.0.unwrap().len() * size_of_val(&0u16)).try_into()?,
222+
(message.0.expect("message buffer is always Some").len() * size_of_val(&0u16)).try_into()?,
223223
windows::Win32::UI::WindowsAndMessaging::MB_SYSTEMMODAL
224224
| windows::Win32::UI::WindowsAndMessaging::MB_SETFOREGROUND,
225225
timeout_in_seconds,

0 commit comments

Comments
 (0)