Skip to content
DoS Audit finding Beginner

SetLib.remove_item() is not safe on empty Dynamic arrays

The library's remove_item() function assumes the dynamic array always has at least one element, so calling it on an empty array underflows the length calculation and attempts to pop from an empty array. This causes a runtime error or revert, which could permanently break any contract functionality that relies on the function with an empty set.

danvinci_20 Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#49075 [SC-Low] `SetLib.remove_item()` is not safe on empty Dynamic arrays

Submitted on Jul 11th 2025 at 11:43:00 UTC by @danvinci_20 for Audit Comp | Folks Smart Contract Library

Description

Brief/Intro

The Uint64SetLib library implements a remove_item() function that aims to provide efficient, linear-time unordered removal of an element from a dynamic array of UInt64. It does this by using a swap-and-pop mechanism, replacing the matched element with the last one and calling .pop() to reduce the array length.

However, the implementation assumes that the array always contains at least one item, and makes two critical unsafe operations:

Python
@>> last_idx = items.length - 1
@>> last_item = items.pop()

If items.length == 0, these operations will result in either underflow or an attempt to pop from an empty array both of which can raise runtime errors or exceptions, depending on the environment. This makes the library function unsafe in general-purpose or reusable contexts where empty arrays might be passed.

While the function is logically correct for non-empty arrays, a defensive library implementation like Openzeppelin EnumerableSet must safely handle all valid inputs, including edge cases such as an empty set. This is especially important in smart contracts and subroutines, where an unexpected revert or exception can cascade and disrupt critical processes.

References

You can check how the Openzeppelin library handles it, it makes use of +1 offset to handle such cases making it more robust: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/structs/EnumerableSet.sol

Recommendation

The function should explicitly check if the array is empty before attempting to remove an element. A simple and safe fix would be:

Python
@subroutine
def remove_item(to_remove: UInt64, items: DynamicArray[ARC4UInt64]) -> Tuple[Bool, DynamicArray[ARC4UInt64]]:
    if items.length == UInt64(0):
        return Bool(False), items.copy()

    last_idx = items.length - 1

    for idx, item in uenumerate(items):
        if item.native == to_remove:
            last_item = items.pop()

            if idx != last_idx:
                items[idx] = last_item

            return Bool(True), items.copy()

    return Bool(False), items.copy()

Proof of Concept

Proof of Concept

Below is the original implementation of the function:

@subroutine
def remove_item(to_remove: UInt64, items: DynamicArray[ARC4UInt64]) -> Tuple[Bool, DynamicArray[ARC4UInt64]]:
    last_idx = items.length - 1

    for idx, item in uenumerate(items):
        if item.native == to_remove:
            last_item = items.pop()

            if idx != last_idx:
                items[idx] = last_item

            return Bool(True), items.copy()

    return Bool(False), items.copy()

If items is an empty array, items.length - 1 underflows, and items.pop() causes a runtime error.

The original report as published on Immunefi, by danvinci_20. 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

Likely the function computes the last index and pops the last element without first checking whether the array is empty.

Impact

Calling the function on an empty set causes a runtime error or revert, which could permanently deny service to a contract feature that uses it.

What the attacker needed

Caller passes an empty array

The fix

The function should check whether the array is empty and return early before computing the last index or popping.

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.

A smart contract library has a helper that removes a number from a list. The helper assumes the list is never empty, so when it is empty the maths goes wrong and the program crashes. In a contract, a crash like this can stop a feature from working at all.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by danvinci_20

View all

22 more disclosed reports, best first.

More from Folks Smart Contract Library

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.