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

refactor(go): avoid using unsafe.Pointer for passing handles #195

Merged
merged 3 commits into from
Sep 14, 2024

Conversation

plusvic
Copy link
Member

@plusvic plusvic commented Sep 14, 2024

Functions that receive a void* in the C world must be called with an unsafe.Pointer from the Go world. However, we are not really passing a pointer, but a "handle", and coercing a handle into an unsafe.Pointer is problematic (go vet complains with "possible misuse of unsafe.Pointer", and it could cause a panic as described in https://i.hsfzxjy.site/invalid-pointer-will-bite-you/).

The solution is introducing small C stub functions that receive an uintptr_t instead of a void*, and the call the original function, casting the uintptr_t into void*.

…ack functions.

Functions that receive a `void*` in the C world must be called with an `unsafe.Pointer` from the Go world. However, we are not really passing a pointer, but a "handle", and coercing a handle into an `unsafe.Pointer` is problematic (`go vet` complains with "possible misuse of unsafe.Pointer", and it could cause a panic as described in https://i.hsfzxjy.site/invalid-pointer-will-bite-you/).

The solution is introducing small C stub functions that receive an `uintptr_t` instead of a `void*`, and the call the original function, casting the `uintptr_t` into `void*`.
@plusvic plusvic merged commit 7a138d5 into compiler-features Sep 14, 2024
31 checks passed
@plusvic plusvic deleted the fix branch September 14, 2024 18:35
plusvic added a commit that referenced this pull request Sep 14, 2024
Functions that receive a `void*` in the C world must be called with an `unsafe.Pointer` from the Go world. However, we are not really passing a pointer, but a "handle", and coercing a handle into an `unsafe.Pointer` is problematic (`go vet` complains with "possible misuse of unsafe.Pointer", and it could cause a panic as described in https://i.hsfzxjy.site/invalid-pointer-will-bite-you/).

The solution is introducing small C stub functions that receive an `uintptr_t` instead of a `void*`, and the call the original function, casting the `uintptr_t` into `void*`.
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.

2 participants