Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

fix: unsafe pointer passing #18

Merged
merged 1 commit into from
Aug 27, 2021
Merged

fix: unsafe pointer passing #18

merged 1 commit into from
Aug 27, 2021

Conversation

Stebalien
Copy link
Member

@Stebalien Stebalien commented Aug 27, 2021

Use github.com/mattn/go-pointer to save/restore "pointers" across FFI bounderies. Go reserves the right to move pointers, so using unsafe.Pointer for this is not safe.

Use github.com/mattn/go-pointer to save/restore "pointers" across FFI
bounderies. Go reserves the right to move pointers, so using
`unsafe.Pointer` for this is not safe.
Copy link

@marten-seemann marten-seemann left a comment

Choose a reason for hiding this comment

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

Assuming that mattn/go-pointer does the right thing (and we're not just moving the stuff that go vet is complaining about into a 3rd party dependency, which go vet then doesn't see any more), this LGTM.
Unfortunately, the README of that repo is not very enlightening.

@Stebalien
Copy link
Member Author

It's doing the right thing. Basically, it's:

  1. Allocating (with cgo) a single byte and using that as the unsafe.Pointer.
  2. Mapping that pointer to the go object.

@Stebalien Stebalien merged commit a04acfd into master Aug 27, 2021
@Stebalien Stebalien deleted the fix/pointer-passing branch August 27, 2021 21:49
oleg-jukovec added a commit to oleg-jukovec/go-openssl that referenced this pull request May 31, 2022
The error occurs when trying to build the library using go < 1.16:
./alloc.go:14:10: unrecognized Go type _Ctype_void
./alloc.go:16:21: unrecognized Go type _Ctype_void

This is an unnecessary restriction. It can be fixed by replacing
*C.void with unsafe.Pointer (as it is already done for `ptr` argument of
CRYPTO_EX_free[1][2]).

1. https://www.openssl.org/docs/manmaster/man3/SSL_get_ex_new_index.html
2. https://www.openssl.org/docs/man3.0/man3/CRYPTO_EX_free.html

Related to libp2p#18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants