# BOLDUpgradeAction.sol will fail to upgrade contracts due to error in the perform function

BOLDUpgradeAction.cleanupOldRollup iterates a staker array while forceRefundStaker shrinks it, causing out-of-bounds revert and blocking the upgrade.

- Payout: Audit contest
- Severity: medium
- Program: Arbitrum Foundation
- Found by: SpicyMeatball
- Disclosed: 2024-06-17
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-arbitrum-foundation-findings/issues/5
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-arbitrum-foundation%3AM-02
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:dos

**Target:** BOLDUpgradeAction.sol upgrade contract / old rollup staker list
**Root cause:** cleanupOldRollup caches stakerCount then loops by index, but forceRefundStaker calls turnIntoZombie->deleteStaker which pops the staker from _stakerList, shrinking the array mid-iteration.
**Steps:**
1. Fork mainnet, target old rollup proxy `0x5eF0D09d1E6204141B4d37530808eD19f60FBa35`
2. Call `pause()` as admin
3. Read `stakerCount()`, cap at 50
4. Loop `i` from 0 to stakerCount-1 calling `getStakerAddress(i)`
5. For each staked, non-challenged staker call `forceRefundStaker([addr])` which pops from `_stakerList`
6. Next `getStakerAddress(i)` reverts with array out-of-bounds panic
**How found:** Read the perform/cleanupOldRollup flow, then traced forceRefundStaker into RollupCore.turnIntoZombie/deleteStaker and noticed _stakerList.pop() shrinks the array the outer loop indexes into.
**Insight:** When auditing migration/cleanup loops that call external functions, check whether those calls mutate the collection being iterated (pop/swap-remove) — cached length plus index iteration breaks on any removal.

## Proof of concept (copied exactly from the original report)

**Foundry mainnet-fork PoC reproducing array out-of-bounds**

```
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import {Test} from "forge-std/Test.sol";
import "forge-std/console.sol";

struct OldStaker {
    uint256 amountStaked;
    uint64 index;
    uint64 latestStakedNode;
    // currentChallenge is 0 if staker is not in a challenge
    uint64 currentChallenge; // 1. cannot have current challenge
    bool isStaked; // 2. must be staked
}

interface IOldRollup {
    function pause() external;
    function forceRefundStaker(address[] memory stacker) external;
    function getStakerAddress(uint64 stakerNum) external view returns (address);
    function stakerCount() external view returns (uint64);
    function getStaker(address staker) external view returns (OldStaker memory);
}

contract C4 is Test {
    IOldRollup oldRollup;
    address admin;
    function setUp() public {
        uint256 forkId = vm.createFork("https://rpc.ankr.com/eth");
        vm.selectFork(forkId);
        oldRollup = IOldRollup(0x5eF0D09d1E6204141B4d37530808eD19f60FBa35);
        admin = 0x3ffFbAdAF827559da092217e474760E2b2c3CeDd;
    }

    function test_Cleanup() public {
        vm.startPrank(admin);
        oldRollup.pause();
        uint64 stakerCount = oldRollup.stakerCount();
        // since we for-loop these stakers we set an arbitrary limit - we dont
        // expect any instances to have close to this number of stakers
        if (stakerCount > 50) {
            stakerCount = 50;
        }
        for (uint64 i = 0; i < stakerCount; i++) {
            // FAILS with panic: array out-of-bounds access 
            address stakerAddr = oldRollup.getStakerAddress(i);
            OldStaker memory staker = oldRollup.getStaker(stakerAddr);
            if (staker.isStaked && staker.currentChallenge == 0) {
                address[] memory stakersToRefund = new address[](1);
                stakersToRefund[0] = stakerAddr;
                oldRollup.forceRefundStaker(stakersToRefund);
            }
        }
    }
}
```

**cleanupOldRollup loop that shrinks staker list**

```
function cleanupOldRollup() private {
        IOldRollupAdmin(address(OLD_ROLLUP)).pause();

>>      uint64 stakerCount = ROLLUP_READER.stakerCount();
        // since we for-loop these stakers we set an arbitrary limit - we dont
        // expect any instances to have close to this number of stakers
        if (stakerCount > 50) {
            stakerCount = 50;
        }
        for (uint64 i = 0; i < stakerCount; i++) {
>>          address stakerAddr = ROLLUP_READER.getStakerAddress(i);
            OldStaker memory staker = ROLLUP_READER.getStaker(stakerAddr);
            if (staker.isStaked && staker.currentChallenge == 0) {
                address[] memory stakersToRefund = new address[](1);
                stakersToRefund[0] = stakerAddr;

                IOldRollupAdmin(address(OLD_ROLLUP)).forceRefundStaker(stakersToRefund);
            }
        }

        // upgrade the rollup to one that allows validators to withdraw even whilst paused
        DoubleLogicUUPSUpgradeable(address(OLD_ROLLUP)).upgradeSecondaryTo(IMPL_PATCHED_OLD_ROLLUP_USER);
    }
```

**forceRefundStaker/turnIntoZombie/deleteStaker shrinking _stakerList**

```
function forceRefundStaker(address[] calldata staker) external override whenPaused {
        require(staker.length > 0, "EMPTY_ARRAY");
        for (uint256 i = 0; i < staker.length; i++) {
            require(_stakerMap[staker[i]].currentChallenge == NO_CHAL_INDEX, "STAKER_IN_CHALL");
            reduceStakeTo(staker[i], 0);
>>          turnIntoZombie(staker[i]);
        }
        emit OwnerFunctionCalled(22);
    }

RollupCore.sol

    function turnIntoZombie(address stakerAddress) internal {
        Staker storage staker = _stakerMap[stakerAddress];
        _zombies.push(Zombie(stakerAddress, staker.latestStakedNode));
>>      deleteStaker(stakerAddress);
    }

    function deleteStaker(address stakerAddress) private {
        Staker storage staker = _stakerMap[stakerAddress];
        require(staker.isStaked, "NOT_STAKED");
        uint64 stakerIndex = staker.index;
        _stakerList[stakerIndex] = _stakerList[_stakerList.length - 1];
        _stakerMap[_stakerList[stakerIndex]].index = stakerIndex;
>>      _stakerList.pop();
        delete _stakerMap[stakerAddress];
    }
```

