-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/vet: go vet
complains unsafe.Slice
usage
#56487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
go vet
complains unsafe.Slice
usagego vet
complains unsafe.Slice
usage
When reading the documentation for
There is no pattern listed that allows what you did in the example. But lets look what you actually trying to do.
As can be extracted from the comment you attempt to convert a "C string" to a Go string. Thankfully there is already a function that does this for you as part of cgo in C.GoString. In the second case
you copy the contents of a buffer from the Objective-C heap to Go. There is also a cgo function for that called C.GoBytes but that would return a newly allocated copy so instead I'd suggest you simply use C.memcpy instead of a Go copy here. This way you avoid having to create Go slices. Hope this was helpful. |
Unfortunately Cgo is not our option. We aim to avoid Cgo by using purego. So we want to treat a uintptr value as a data source. We could do it with |
I'm pretty sure the As an alternative you can copy each byte manually but this will likely perform worse because these loops will not be vectorized/unrolled. So to get an optimized copy you have options
|
In the above NSString case, the value points to a C heap, but the type is uintptr. A uintptr as a C address is very often used e.g. when low-level APIs like syscall.Syscall is used. It's OK that this can be like an undefined behavior in C, as this is an 'unsafe' package. Using an alternative way to just avoid go-vet warnings doesn't solve the root problem. Actually if we just wanted to avoid warnings, we could use |
It's true that the unsafe package, and therefore vet, has an exception for CC @adonovan |
The exception for Note that it is, in fact, possible to silence this var foo uintptr = 0x12345678
_ = unsafe.Slice(*(**byte)(unsafe.Pointer(&foo)), 0) |
I agree that the use is unsafe. That said, one of the goals of |
The Moreover, silencing the Especially given how trivial the workaround is, I don't think it's worth trying to fine-tune the |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
and run
go vet
What did you expect to see?
No complaints
What did you see instead?
When
reflect.SliceHeader
is used, there is no such a problem since theData
field isuintptr
and we could use a uintptr value without warnings. So this sounds like a kind of regression.The text was updated successfully, but these errors were encountered: