Skip to content
Latchkey

Go vet "possible misuse of unsafe.Pointer" - Fix in CI

The unsafe.Pointer rules require that pointer arithmetic happen in a single expression so the GC never sees a bare uintptr holding a live address. vet flags conversions that store uintptr across statements.

What this error means

go vet fails with possible misuse of unsafe.Pointer. A uintptr was held in a variable between converting from and back to unsafe.Pointer, which is not GC-safe.

go
./mem.go:14:9: possible misuse of unsafe.Pointer

Common causes

uintptr stored across statements

A pointer was converted to uintptr, kept in a variable, then converted back later, leaving a window where the GC could move the object.

Arithmetic split into steps

Pointer offset math was broken into multiple statements instead of a single expression, violating the unsafe rules.

How to fix it

Keep the conversion in one expression

  1. Do the offset arithmetic and the back-conversion in a single expression so no uintptr persists.
Go
p := unsafe.Pointer(uintptr(base) + offset)

Prefer safe abstractions

  1. Replace manual pointer math with unsafe.Add or a typed slice where possible.
Go
p := unsafe.Add(base, offset)

How to prevent it

  • Never store an address in a uintptr variable across statements.
  • Use unsafe.Add and unsafe.Slice instead of manual arithmetic.
  • Run go vet in CI so unsafe misuse is caught early.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →