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

fix(win): fix HSTRING being dropped, causing NERR_UserNotFound errors #1414

Merged
merged 1 commit into from
Dec 7, 2024

Conversation

ehuff700
Copy link
Contributor

@ehuff700 ehuff700 commented Dec 6, 2024

In an attempt to fix #1233, a pull request #1359 was made which introduced broken code into the repo, shown below:

#[cfg(any(feature = "user", feature = "system"))]
pub(crate) unsafe fn to_pcwstr(v: Vec<u16>) -> windows::core::PCWSTR {
    if v.is_empty() {
        return windows::core::PCWSTR::null();
    }
    let string = String::from_utf16(&v).unwrap_or_default();
    // New string allocated here with format!(), but data in the HSTRING is also dropped here (tested with breakpoints)
    windows::core::PCWSTR(windows::core::HSTRING::from(&format!("{}\0", string)).as_ptr())
}

This caused inconsistent behavior when returning groups for users, as the data in the HSTRING was freed before it could be used in the get_groups_for_user function. Additionally, HSTRING internals already terminate given strings with a null byte, making the code above irrelevant.

This PR ensures that the Vec data is appropriately null terminated and the heap memory it points to will last for the duration of the get_groups_for_user function, as expected.

@GuillaumeGomez
Copy link
Owner

Super cool, thanks!

@GuillaumeGomez GuillaumeGomez merged commit de44c91 into GuillaumeGomez:master Dec 7, 2024
65 of 67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to Retrieve User Groups on Windows
2 participants