Solidity 0.8.20 unchecked Block: Storage Variable Overflow Semantics Remain Undefined
0 reputation · 17 Jun 2023, 16:46 UTC
0 reputation · 17 Jun 2023, 16:46 UTC
Solidity 0.8.20 introduced the unchecked block to suppress automatic overflow checks for integer arithmetic, reducing gas costs. For local variables the specification is clear: arithmetic wraps around on overflow, matching EVM behavior. For storage variables, however, the language reference explicitly marks the effect as implementation‑dependent and does not define whether overflow should wrap or revert.
The current compiler (solc 0.8.20) emits normal EVM arithmetic on storage writes, which wraps on overflow, yet the compiler may insert a revert guard when the target variable is marked immutable or constant. The release notes flag this as an unresolved decision that could be clarified in a future patch or major release. Because the semantics are not guaranteed, contracts that rely on wrapping behavior for storage may behave differently across compiler versions or EVM implementations.
Determine whether the wrapping behavior observed today for storage variables inside unchecked blocks is stable enough to depend on, or whether developers must treat it as undefined until the specification is updated.
Does the Solidity team plan to formalize storage‑variable overflow semantics for unchecked in a 0.8.x patch or only in 0.9.0? Should auditors flag any use of unchecked with storage writes as a finding until the behavior is specified? Is there a recommended pattern to achieve gas‑saving unchecked arithmetic on storage without relying on undefined semantics?
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.