Skip to content
Latchkey

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.

go vet output
./buffer.go:21:9: possible misuse of unsafe.Pointer

Common 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.Pointeruintptr→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.

Go
// safe: single expression, no intermediate uintptr variable
p2 := unsafe.Pointer(uintptr(unsafe.Pointer(p)) + offset)

Avoid unsafe where the standard library suffices

  1. Check whether encoding/binary, slices, or generics can replace the unsafe code.
  2. Reserve unsafe for genuine interop or performance-critical paths.
  3. Re-run go vet ./... to confirm the warning is gone.

How to prevent it

  • Follow the unsafe.Pointer rules: convert and arithmetic in one expression.
  • Keep go vet (with unsafeptr) in CI.
  • Prefer safe stdlib alternatives over unsafe whenever possible.

Frequently asked questions

What causes ""possible misuse of unsafe.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.
How do I fix "possible misuse of unsafe.Pointer"?
Do the uintptr arithmetic and the conversion back to unsafe.Pointer in one expression so the GC cannot intervene.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card