Go vet "possible misuse of unsafe.Pointer" - Fix in CI
The unsafeptr vet check flags conversions that turn a uintptr back into an unsafe.Pointer in ways the garbage collector cannot track. Such code can read freed or moved memory, so vet fails the build to force a safe pattern.
What this error means
A go vet ./... step fails with possible misuse of unsafe.Pointer, pointing at a conversion through uintptr. The code compiles and may even appear to work, but vet flags it as a memory-safety hazard.
./buffer.go:21:9: possible misuse of unsafe.PointerCommon causes
A uintptr stored then converted back to a Pointer
Saving uintptr(unsafe.Pointer(p)) in a variable and later converting it back is unsafe: the GC may move or free the object between the two steps.
Pointer arithmetic split across statements
Doing the unsafe.Pointer→uintptr→arithmetic→unsafe.Pointer dance in separate statements lets the GC act in between, which vet detects.
How to fix it
Keep the conversion in a single expression
Do the uintptr arithmetic and the conversion back to unsafe.Pointer in one expression so the GC cannot intervene.
// safe: single expression, no intermediate uintptr variable
p2 := unsafe.Pointer(uintptr(unsafe.Pointer(p)) + offset)Avoid unsafe where the standard library suffices
- Check whether
encoding/binary, slices, or generics can replace the unsafe code. - Reserve
unsafefor genuine interop or performance-critical paths. - Re-run
go vet ./...to confirm the warning is gone.
How to prevent it
- Follow the
unsafe.Pointerrules: convert and arithmetic in one expression. - Keep
go vet(with unsafeptr) in CI. - Prefer safe stdlib alternatives over
unsafewhenever possible.