Exceeded Shift

ID

go.exceeded_shift

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

reliability, suspicious

Description

Reports a shift x << n or x >> n where n is an integer literal at least as large as the bit width of x’s sized integer type, for example `u32 << 33 or i8 >> 9. Shifting by the width or more discards every bit, so the result is a constant.

Rationale

A shift count equal to or greater than the operand width yields a constant result (0, or -1 for an arithmetic right shift of a negative value), which is almost always a mistake in the count or in the operand’s type. The check is conservative: the count must be an integer literal, the operand must resolve to a sized integer type, and the platform-dependent int / uint types are treated as 64 bits so only a count beyond the largest possible width is reported.

var a uint32 = 1; _ = a << 33 // FLAW — 33 ≥ 32
var b int8 = 1;   _ = b >> 9  // FLAW — 9 ≥ 8

_ = a << 31                   // OK — within width

Remediation

Use a shift count smaller than the operand’s bit width, or widen the operand to a type that can hold the intended result.