Hi,
There is something that puzzles me a lot in the doc about cgo.Handle there 
https://pkg.go.dev/runtime/cgo.  It says:


*Some C functions accept a void* argument that points to an arbitrary data 
value supplied by the caller. It is not safe to coerce a cgo.Handle (an 
integer) to a Go unsafe.Pointer, but instead we can pass the address of the 
cgo.Handle to the void* parameter, as in this variant of the previous 
example: *

I was under the impression that casting a uintptr to an  unsafe.Pointer 
just to call a C function (that accepts a  void*) is somewhat valid. (This 
is not clearly  specified as a valid case in the doc of unsafe.Pointer, so, 
it might be invalid from a very pedantic point of view).

The advice given in the doc looks worst.  Taking the address of a local 
variable and giving it to C looks even more undefined (if C stores the 
pointer and reuse it after the original call returns).

Don't we have a doc issue there ?

I know that in C, the conversion uintptr -> void* is subject to debate 
(trap representation, ...).  In practice, nobody cares because *It just 
works*. (example of the nobody 
cares 
https://www.freedesktop.org/software/gstreamer-sdk/data/docs/latest/glib/glib-Type-Conversion-Macros.html)

In practice, I'm almost certain that the naïve code that just gives the 
cgo.Handle to a C function as a void* is *much* safer that the pattern 
suggested in the doc.

So should the doc be changed, or am I missing some important piece ?

Thanks
Fred

-- 
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/49cadd8f-89b4-4f91-8ad3-241768758dc3n%40googlegroups.com.

Reply via email to