Skip to content

Commit

Permalink
Merge pull request #1074 from GuillaumeGomez/win-fix-null-ptr-use
Browse files Browse the repository at this point in the history
Windows: Fix invalid use of NULL pointer
  • Loading branch information
GuillaumeGomez authored Sep 24, 2023
2 parents 7cdab25 + da800dc commit 87d1c85
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/windows/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ pub struct User {
pub(crate) uid: Uid,
pub(crate) gid: Gid,
pub(crate) name: String,
c_user_name: Vec<WCHAR>,
c_user_name: Option<Vec<WCHAR>>,
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);
Expand All @@ -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 {
Expand Down Expand Up @@ -77,10 +77,11 @@ impl UserExt for User {

fn groups(&self) -> Vec<Group> {
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()
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/windows/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 87d1c85

Please sign in to comment.