Skip to content

Iterating / Deleting Large Storage Mapping-Array

SAFE-0241 Denial of Service Checked automatically by the scanner
1

What goes wrong

A function deletes or rewrites every element of a growing storage array in one call. Past a certain size the operation cannot fit in a block, so it can never complete — the state becomes permanently unmanageable.

2

The vulnerable pattern

proof of concept — how it is exploited
// reset() { delete bigArray; } // O(n) storage writes
// once bigArray has ~thousands of entries, reset() exceeds block gas
// -> the protocol can never be reset.
3

How to fix it

the pattern that is safe
// process in bounded chunks across multiple transactions
function clear(uint256 n) external { for (uint i; i<n && arr.length>0; ++i) arr.pop(); }
Bound any O(n) storage operation.
Check your own contract for this

The SaferICO scanner runs 201 detectors over your Solidity source, SAFE-0241 among them. Paste an address or the source itself — a small per-scan fee, shown before you sign, or unlimited on any plan.

Run the scanner See how it is attacked Read the docs