Skip to content
Business logic Audit finding Beginner

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.

AasifUsmani Follow

Originally disclosed on Immunefi

Informational View original
  • 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

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]:

Solidity
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 — only pop() is required. In that case, lastStaker is 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:

Solidity
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

https://github.com/immunefi-team/attackathon-plume-network/blob/580cc6d61b08a728bd98f11b9a2140b84f41c802/plume/src/lib/PlumeValidatorLogic.sol#L94

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 %}

The original report as published on Immunefi, by AasifUsmani. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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.

More by AasifUsmani

View all

8 more disclosed reports, best first.

More from Plume Network

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.