Atomic Assignment

ID

go.atomic_assignment

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

concurrency, reliability

Description

Reports an assignment that stores the result of a sync/atomic operation back into the very variable that operation targets, for example x = atomic.AddUint64(&x, 1).

Rationale

The atomic call already updates x atomically through the pointer. Assigning its return value back to x adds a second, non-atomic write to the same location. Between the atomic update and the plain store, another goroutine can write x, and that write is then silently lost — defeating the purpose of using atomics in the first place.

// Bad
x = atomic.AddUint64(&x, 1)   // FLAW — racy non-atomic store back to the atomic target

// Good
y := atomic.AddUint64(&x, 1)  // OK — result kept in a separate variable
atomic.AddUint64(&x, 1)       // OK — no store-back; x is updated in place

Remediation

Remove the assignment and rely on the atomic call’s in-place update, or store its return value in a different variable.