Before Solidity 0.8.0, dynamic arrays lacked length underflow protection. When a dynamic array's length was decreased below zero through array.length--, it would wrap around to 2^256 - 1, effectively granting write access to the entire contract storage.
Storage Layout Fundamentals
In Solidity, storage slots are 32-byte positions numbered from 0 to 2^256 - 1. Dynamic arrays store their length at a specific slot p, while their elements are stored starting at keccak256(p).
Example with a dynamic array at slot 1:
Slot 1: stores the array length
Slot keccak256(1): stores array[0]
Slot keccak256(1) + 1: stores array[1]
Slot keccak256(1) + n: stores array[n]
The Underflow Attack Vector
When array.length underflows from 0 to 2^256 - 1, the array theoretically spans the entire storage space. An attacker can then calculate offsets to overwrite any storage slot, including critical variables like contract ownership or access control flags.
By solving for the offset, an attacker can write to arbitrary storage locations through seemingly legitimate array access operations.
Practice
When auditing pre-0.8.0 contracts, scrutinize any code that allows users to decrease dynamic array lengths, particularly through operations like pop(), manual length--, or delete on array elements in certain contexts.
High-Risk Patterns
Look for contracts where:
Users can trigger array length decrements without proper bounds checking
Dynamic arrays are combined with privileged storage variables in the same contract