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

Avoid clearing SetLastError when allocating string params #1665

Merged
merged 1 commit into from
Apr 6, 2022

Conversation

kennykerr
Copy link
Collaborator

Switched to using the low-level HeapAlloc allocator rather than SysAllocXxx as the former doesn't clear the thread's last error.

Also added a test to ensure the behavior is persevered.

Fixes #1664

@kennykerr kennykerr requested a review from riverar April 6, 2022 17:20
@kennykerr kennykerr merged commit 0bd146d into master Apr 6, 2022
@kennykerr kennykerr deleted the string_param branch April 6, 2022 17:39
@@ -18,3 +18,12 @@ pub unsafe fn heap_free(ptr: RawPtr) {
HeapFree(heap, HEAP_NONE, ptr);
}
}

pub fn heap_string<T: Copy + Default + Sized>(value: &[T]) -> *const T {
let buffer = heap_alloc((value.len() + 1) * std::mem::size_of::<T>()).expect("Could not allocate string") as *mut T;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since the T here is uninitialized this should probably be *mut MaybeUninit<T>. It's probably fine to leave for now though 🤷

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really only want to use this function for u8 and u16. Be nice if Rust had traits for stuff like that. C++ for example has the is_unsigned trait.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the pointer points to uninit memory, the next line (where you make the slice) is UB.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to suggest a better implementation or shoot me a PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request opened here: #1667

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kenny that's an unfortunate way to respond to UB reports. I encourage you to consider them a serious bug even if the person who points it out doesn't provide a solution at the same time. There's several ways to deal with uninit data in Rust, and rylev had already mentioned one of them for you just above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't trying to minimize the issue. I just wasn't sure how to address it. Sorry for the confusion.

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.

Bug: 0.35.0 clears GetLastError on somewhere
4 participants