From da800dc95c889a6fd83860df61ee3665345cfa0f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 24 Sep 2023 14:33:51 +0200 Subject: [PATCH] Windows: Fix invalid use of NULL pointer --- src/windows/users.rs | 15 ++++++++------- src/windows/utils.rs | 5 ++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/windows/users.rs b/src/windows/users.rs index 8b1138f30..bb706eb77 100644 --- a/src/windows/users.rs +++ b/src/windows/users.rs @@ -28,14 +28,14 @@ pub struct User { pub(crate) uid: Uid, pub(crate) gid: Gid, pub(crate) name: String, - c_user_name: Vec, + c_user_name: Option>, is_local: bool, } impl User { fn new(uid: Uid, name: String, c_name: LPWSTR, is_local: bool) -> Self { unsafe { - let c_user_name = if is_local { + let c_user_name = if is_local && !c_name.is_null() { let mut i = 0; loop { let c = *c_name.offset(i); @@ -45,10 +45,10 @@ impl User { } i += 1; } - Vec::from(std::slice::from_raw_parts(c_name, i as _)) + Some(Vec::from(std::slice::from_raw_parts(c_name, i as _))) } else { // There is no local groups for a non-local user. - Vec::new() + None }; Self { @@ -77,10 +77,11 @@ impl UserExt for User { fn groups(&self) -> Vec { if self.is_local { - unsafe { get_groups_for_user(self.c_user_name.as_ptr() as _) } - } else { - Vec::new() + if let Some(c_user_name) = &self.c_user_name { + unsafe { return get_groups_for_user(c_user_name.as_ptr() as _) }; + } } + Vec::new() } } diff --git a/src/windows/utils.rs b/src/windows/utils.rs index 3ed7e2113..699fe1217 100644 --- a/src/windows/utils.rs +++ b/src/windows/utils.rs @@ -24,8 +24,11 @@ pub(crate) fn get_now() -> u64 { } pub(crate) unsafe fn to_str(p: LPWSTR) -> String { - let mut i = 0; + if p.is_null() { + return String::new(); + } + let mut i = 0; loop { let c = *p.offset(i); if c == 0 {