Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Fix invalid use of NULL pointer #1074

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading