Address Compared To Nil
ID |
go.address_compared_to_nil |
Severity |
high |
Remediation Complexity |
simple |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Go |
Tags |
CWE:570, reliability, suspicious |
Description
Reports a comparison of an address-of expression against nil — &x == nil or &x != nil. The
address of an addressable value is never nil, so the comparison has a constant result.
Rationale
&x == nil is always false and &x != nil is always true. Such a guard is dead code and almost
always a mistake — the author likely meant to compare a pointer variable itself, not its address.
The check fires only when one whole operand is an address-of expression and the other is the bare
nil literal.
func fn(x int, p *int) {
if &x != nil { // FLAW — always true
println("obviously")
}
if p == nil { // OK — comparing a pointer variable
}
}