Skip to main content

SKY-G206 · Go-specific rules

Go unsafe package use

Low

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

store/cache.go · GoReported on line 21
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

store/cache_test.go · Go
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

  1. Runs with --danger. The bundled Go engine, skylos-go, parses each .go file with Go's own parser. _test.go files, symbolic links and files under vendor, testdata, node_modules and hidden folders are skipped. A go.mod is not required.
  2. 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.String and the rest.
  3. A type written as unsafe.Pointer (a field or a variable declaration) is not a call and is not reported.
  4. 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.

store/cache.go · Go · the fix, against the flagged file
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.StringHeader and //go:linkname carry similar risks without a call into unsafe and 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 a pyproject.toml at 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.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.