Why all the conversions? Why not just pass the C pointer from one package 
to another? Or an unsafe.Pointer? You can use a placeholder type, even. 
Make procAddr a *byte, for example.

The pointer conversion rules are intended to ensure safety when playing 
with Go pointers (by that, I mean pointers to objects in the Go heap or 
stack). They're definitely overkill for pointers that you know point into 
the C heap. Unfortunately, the vet rules don't know where the pointers came 
from.

On Thursday, February 13, 2020 at 9:37:47 AM UTC-8 Alex wrote:

> I have to use a massive third party API so it's not really a choice to use 
> cgo.
>
> Do you have any safer options to pass C pointers between packages?
> The C pointers are all C allocated and package B needs to call C functions 
> directly and pass those pointers.
>
> On Friday, 14 February 2020 01:02:24 UTC+8, Jake Montgomery wrote:
>>
>> You need to read https://golang.org/pkg/unsafe/#Pointer *very, very, 
>> very* carefully before using unsafe.Pointer in any way. It spells out 6 
>> conversions that are considered "valid". It says: "Code not using these 
>> patterns is likely to be invalid today or to become invalid in the future." 
>> AFAICT, your code does not fit any of those 6 allowable patterns. 
>>
>> Go is simple and easy, CGO is tricky, difficult and full of dragons.
>>
>>
>> On Thursday, February 13, 2020 at 11:42:30 AM UTC-5, Alex wrote:
>>>
>>> I have to pass C pointers between packages so I used uintptr like how 
>>> syscall does things.
>>> However go vet gives the message "possible misuse of unsafe.Pointer". 
>>>
>>> Is there something I could do to avoid vet complaining?
>>>
>>> Package A:
>>> type Foo struct {
>>> procAddr uintptr
>>> }
>>>
>>> func (f Foo) ProcAddr() uintptr {
>>> return f.procAddr
>>> }
>>>
>>> func Bar() Foo {
>>>         return Foo{C.SomeFunc()}
>>> }
>>>
>>> Package B:
>>> p := C.ASDF(unsafe.Pointer(A.Bar().ProcAddr())) // possible misuse of 
>>> unsafe.Pointer
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/497901a6-3aad-45a0-a261-890b1abe230c%40googlegroups.com.

Reply via email to