cmd/cgo: clarify safety of calling unsafe.Slice and unsafe.String with C pointers #68832
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Go version
1.22.6
Output of
go env
in your module/workspace:What did you do?
N/A
What did you see happen?
The documentation for cmd/cgo#Passing pointers states that strings and slices always contain Go pointers:
This seems to imply that the following is UB, as it constructs a slice from a C pointer rather than a Go pointer:
However, discussion on the Gophers Slack suggests that this may actually be perfectly fine, as Go should still know whether a pointer points to memory managed by Go. syscall.Mmap was mentioned as an example of a slice not backed by Go-managed memory.
What did you expect to see?
Accurate information in the Cgo documentation regarding the safety of strings and slices backed by C-managed memory.
In either case, an explicit description of the safety or non-safety of passing a C pointer would be appreciated in the documentation for
unsafe.String
andunsafe.Slice
.As an aside, it would also be good to document any other invariants that must be upheld by the data passed to
unsafe.String
, if applicable. In particular, whether the string must be valid UTF-8, and whether it may contain null bytes.The text was updated successfully, but these errors were encountered: