SKY-G206 · Go-specific rules
Go unsafe package use
Go's unsafe package lets code read and write memory outside the type system, for example by turning any pointer into any other. SKY-G206 reports each call into unsafe, such as unsafe.Pointer(p), unsafe.Sizeof(x) or unsafe.String(...), so a person reviews it. The catalog calls the rule "Go unsafe package import", but an import alone is not reported.
- Languages
- Go
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Go unsafe package import
Why it matters
A wrong unsafe conversion is not caught by the compiler or the race detector. It shows up as corrupted data or a crash much later, often only under load, and it can break when the Go version or the struct layout changes.
What Skylos flags
package store import ( "sync/atomic" "unsafe") type Snapshot struct { Prices map[string]int64} type Cache struct { current unsafe.Pointer} func (c *Cache) Load() *Snapshot { return (*Snapshot)(atomic.LoadPointer(&c.current))} func (c *Cache) Store(s *Snapshot) { atomic.StorePointer(&c.current, unsafe.Pointer(s))}What Skylos leaves alone
package store import ( "testing" "unsafe") func TestCacheIsOnePointer(t *testing.T) { if size := unsafe.Sizeof(Cache{}); size != unsafe.Sizeof(uintptr(0)) { t.Fatalf("Cache is %d bytes, want one pointer", size) }}Why Skylos leaves it alone
The calls are in a _test.go file, which only go test compiles. The Go engine does not scan test files, so test-only uses of unsafe are not reported.
How Skylos decides
- Runs with
--danger. The bundled Go engine,skylos-go, parses each.gofile with Go's own parser._test.gofiles, symbolic links and files undervendor,testdata,node_modulesand hidden folders are skipped. Ago.modis not required. - Resolves each call's package through the file's imports and reports every call whose package is
unsafe:unsafe.Pointer(...),unsafe.Sizeof,unsafe.Offsetof,unsafe.Add,unsafe.Slice,unsafe.Stringand the rest. - A type written as
unsafe.Pointer(a field or a variable declaration) is not a call and is not reported. - Reports LOW on the line of the call. The ID stays SKY-G206 in the output; it is not renamed to a cross-language rule.
How to fix it
Use the typed sync/atomic.Pointer[T] (Go 1.19 and later) instead of converting through unsafe.Pointer. It does the same lock-free swap and the compiler checks the type.
package store Removed: import (Removed: "sync/atomic"Removed: "unsafe"Removed: )Added: import "sync/atomic" type Snapshot struct { Prices map[string]int64} type Cache struct {Removed: current unsafe.PointerAdded: current atomic.Pointer[Snapshot]} func (c *Cache) Load() *Snapshot {Removed: return (*Snapshot)(atomic.LoadPointer(&c.current))Added: return c.current.Load()} func (c *Cache) Store(s *Snapshot) {Removed: atomic.StorePointer(&c.current, unsafe.Pointer(s))Added: c.current.Store(s)}When it can be wrong
- Reviewed, deliberate uses in low-level code (a zero-copy parser, a binding to C) are reported each time; that is the point of a review prompt, but it adds noise in such packages.
reflect.SliceHeader,reflect.StringHeaderand//go:linknamecarry similar risks without a call intounsafeand are not reported.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
At the end of the reported line; a comment on the line above is not read:
Go atomic.StorePointer(&c.current, unsafe.Pointer(s)) // skylos: ignore[SKY-G206] reviewed[tool.skylos] ignore = ["SKY-G206"]in apyproject.tomlat the project root turns the rule off. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.