Bit Shift Overflow Remediation
Overview
Related Detector: Bit Shift Overflow
Shifting a 256-bit value by 256 or more bits yields zero in the EVM, and shift operations are not covered by Solidity 0.8’s overflow checks. If the shift amount comes from user input or a computed value that can exceed the operand width, an attacker can force an intermediate to zero — and a zeroed denominator, price, or threshold is a critical math error. The remediation is to bound the shift amount before shifting.
Recommended Fix
Before (Vulnerable)
function scale(uint256 x, uint256 shift) external pure returns (uint256) {
// If shift >= 256, (1 << shift) is 0 and the division reverts or corrupts.
return x / (1 << shift);
}
After (Fixed — bound the shift)
error ShiftTooLarge();
function scale(uint256 x, uint256 shift) external pure returns (uint256) {
if (shift >= 256) revert ShiftTooLarge();
return x / (1 << shift);
}
Where the shift is meant to select a fixed-point scale, validate it against the specific expected range rather than only the 256-bit ceiling.
Common Mistakes
- Assuming Solidity 0.8 reverts on an oversized shift — it does not; only arithmetic operators are checked.
- Bounding a
<<but leaving a paired>>unbounded, so the inverse operation still zeroes out. - Using the zeroed result as a divisor, turning a silent shift bug into a revert-based denial of service.