Skip to content

x/tools/go/passes/unsafeptr: detect "liveness holes" #76660

@adonovan

Description

@adonovan

The code below forms a new pointer y by adding an offset to another pointer ptr:

y := uintptr(unsafe.Pointer(ptr)) + uintptr(n)
return (*byte)(unsafe.Pointer(y))

If ptr is the sole reference to a variable, then after the assignment to y is complete, there are no more references to that variable and the GC is free to reclaim its storage. By the time the new pointer is formed in the return statement, the variable may no longer exist.

The correct code either inlines the expression for y into the return statement, or moves the unsafe.Pointer(y) conversion into the definition of y, so that after every statement, the variable is live.

Although the existing unsafeptr package detects some pointer mistakes, it does not detect this one. It should.

See also:

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsFixThe path to resolution is known, but the work has not been done.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions