Skip to content

cmd/vet: flag atomic.Value usages with interface types #22550

Description

@dsnet

Consider the following:

var v atomic.Value
var err error

err = &http.ProtocolError{}
v.Store(err)
err = io.EOF
v.Store(err)

The intention to have a atomic value store for errors. However, running this code panics:

panic: sync/atomic: store of inconsistently typed value into Value

This is because atomic.Value requires that the underlying concrete type be the same (which is a reasonable expectation for its implementation). When going through the atomic.Value.Store method call, the fact that both these are of the error interface is lost.

Perhaps we should add a vet check that flags usages of atomic.Value where the argument passed in is an interface type?

Vet criterions:

  • frequency: not sure, I haven't done an analysis through all Go corpus.
  • correctness: this is almost always wrong. Any "correct" usages should type assert to the concrete value first.
  • accuracy: if the type information available can conclusively show an argument is an interface type, then very accurate.

\cc @robpike
\cc @dominikh for staticcheck

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions