On Thu, Feb 13, 2020 at 8:42 AM Alex <alex.rou...@gmail.com> 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

If the pointers involved are all C pointers--if they all point to
memory allocated in C--then use little wrapper functions in your cgo
comment to do the conversion to uintptr_t on the C side rather than on
the Go side.

/*
static uintptr_t SomeFuncUintptr() { return (uintptr_t)(SomeFunc()); }
*/
import "C"

Then the Go code will only ever see uintptr values, which is what you want.

To reiterate, this is only safe if the C code is only ever using
pointers to memory allocated in C.

Ian

-- 
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/CAOyqgcVTji9nXEpuNyNpXm7Y00s52yNgSi1scwt-xxJAaZf5kg%40mail.gmail.com.

Reply via email to