Redundant array access in removestakerfromvalidator
The removeStakerFromValidator function reads the last staker from storage even when removing the last element, where no swap is needed. This causes an unnecessary storage read that wastes gas but does not affect correctness or security.
Originally disclosed on Immunefi
- Disclosed after under an hour
51651 sc insight redundant array access in removestakerfromvalidator
Submitted on Aug 4th 2025 at 16:13:23 UTC by @AasifUsmani for Attackathon | Plume Network
- Report ID: #51651
- Report Type: Smart Contract
- Report severity: Insight
- Target: https://github.com/immunefi-team/attackathon-plume-network/blob/main/plume/src/lib/PlumeValidatorLogic.sol
Description
Brief/Intro
In the removeStakerFromValidator function of the PlumeValidatorLogic library, an unnecessary array access occurs during the removal of a staker from a validator's list. Specifically, the declaration and access of lastStaker happen unconditionally, even when no swap is needed (i.e., when removing the last element). This leads to a redundant storage read (SLOAD) that can be avoided by moving the access inside the conditional that performs the swap.
Vulnerability Details
The issue is in Part 1 of the function, which uses a swap-and-pop pattern to remove a staker from $.validatorStakers[validatorId]:
if (indexToRemove < listLength && stakersList[indexToRemove] == staker) {
address lastStaker = stakersList[listLength - 1]; // Redundant if no swap is needed
if (indexToRemove != listLength - 1) {
stakersList[indexToRemove] = lastStaker;
$.userIndexInValidatorStakers[lastStaker][validatorId] = indexToRemove;
}
stakersList.pop();
}
- The line
address lastStaker = stakersList[listLength - 1];is executed regardless of whether a swap will occur. - If
indexToRemove == listLength - 1(removing the last element), no swap is needed — onlypop()is required. In that case,lastStakeris never used, making the read unnecessary. - This results in a redundant SLOAD, costing gas without changing behavior.
- Functionally the code is correct; this is a low-risk optimization opportunity.
{% hint style="info" %} Severity: Low / Informational — No security risk or incorrect state change. Optimization and clarity improvement only. {% endhint %}
Impact Details
- Gas Efficiency: Each redundant SLOAD adds gas (roughly 100–200 gas per call). In high-frequency removal scenarios (mass unstaking, bulk operations), this can accumulate.
- Code Quality: Removing the redundant read improves clarity and aligns with standard swap-and-pop patterns in Solidity, reducing potential confusion for maintainers and auditors.
Recommended Fix
Move the lastStaker declaration inside the conditional that performs the swap so it is only read when needed:
if (indexToRemove < listLength && stakersList[indexToRemove] == staker) {
if (indexToRemove != listLength - 1) {
address lastStaker = stakersList[listLength - 1];
stakersList[indexToRemove] = lastStaker;
$.userIndexInValidatorStakers[lastStaker][validatorId] = indexToRemove;
}
stakersList.pop();
}
This avoids the unnecessary storage read when removing the last element.
References
Proof of Concept
Consider a validator's stakersList with 3 elements: [0xA, 0xB, 0xC] (length = 3).
{% stepper %} {% step %}
Remove last element (0xC)
- indexToRemove = 2
- listLength = 3
Current code:
- Reads
lastStaker = stakersList[2](0xC) unconditionally. - Checks
if (2 != 2)→ false, so no swap. - Pops the last element → resulting list: [0xA, 0xB]
Redundancy: lastStaker was read but never used.
{% endstep %}
{% step %}
Optimized flow
With the recommended change:
- The code checks
if (indexToRemove != listLength - 1)first. - Since it's false, it skips reading
stakersList[listLength - 1]. - Calls
pop()→ resulting list: [0xA, 0xB]
Benefit: One less SLOAD (gas saved) when removing the last element. {% endstep %} {% endstepper %}
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The last staker is read from storage before checking whether a swap is actually required.
Impact
Each redundant storage read costs roughly 100–200 gas, which could add up in high-frequency removal scenarios, but there is no security risk or incorrect state change.
What the attacker needed
None
The fix
The last staker should be read only inside the conditional that performs the swap.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This is a gas optimisation report, not a security bug. The code reads a value from the blockchain's storage (which costs gas) even when that value is not needed, so the contract wastes a small amount of gas. Nothing can be stolen or broken; it is just inefficient.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.