An underflow in remove_item function in Uint64SetLib Contract.
The remove_item function in Uint64SetLib computes the last index as the array length minus one without checking whether the array is empty, so calling it on an empty set causes an underflow and an unexpected revert. An attacker or user could trigger this revert, causing a temporary denial of service for one block.
Originally disclosed on Immunefi
- Disclosed after under an hour
#49687 [SC-Low] An underflow in `remove_item` function in `Uint64SetLib` Contract.
Submitted on Jul 18th 2025 at 11:41:38 UTC by @Immanux2160 for Audit Comp | Folks Smart Contract Library
- Report ID: #49687
- Report Type: Smart Contract
- Report severity: Low
- Target: https://github.com/Folks-Finance/algorand-smart-contract-library/blob/main/contracts/library/UInt64SetLib.py
- Impacts:
- Temporary denial of service (smart contract is made unable to operate for one block, functionality is restored in the next block)
Description
Brief/Intro
The remove_item function in Uint64SetLib first line compute last_idx = items.length - 1, this line does not check if the array it is computing is empty(0) and this can lead to an underflow in the system which does revert.
Vulnerability Details
The remove_item function in Uint64SetLib.py compute last_idx = items.length - 1 the first line does not check if the array is empty, which can lead to an underflow. While the implementation is okay, there should be a check for empty(0) array, so as to avoid underflow, and it should also return a revert error, so the user knows that the array is empty instead of being clueless about what is wrong.
Impact Details
Unexpected revert, without knowing what is wrong, the user or developer may be confused whenever they use the remove_item function.
Lack of an error feedback when the user or developer uses the remove_item when the array is empty.
References
We can check how the standard Openzeppelin EnumerableSet Library works:
Proof of Concept
Proof of Concept
This is their current implementation of the remove functionality:
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:
# remove last item to replace the "to_remove" item or remove entirely if it's the match
last_item = items.pop()
if idx != last_idx:
items[idx] = last_item
# return with the item removed
return Bool(True), items.copy()
# if here then item is not present
return Bool(False), items.copy()
I will recommend we should check that the length of the items is not zero and we handle such case gracefully increasing the robustness of the library.
if len(items) == 0:
return Bool(False), items.copy()
else:
for idx, item in uenumerate(items):
if item.native == to_remove:
# remove last item to replace the "to_remove" item or remove entirely if it's the match
last_item = items.pop()
if idx != last_idx:
items[idx] = last_item
# return with the item removed
return Bool(True), items.copy()
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 before verifying that the array is non-empty, so an empty array causes an unsigned integer underflow.
Impact
Calling remove_item on an empty set causes an unexpected revert, resulting in a temporary denial of service for one block and confusing error feedback for users or developers.
What the attacker needed
Calling remove_item on an empty set
The fix
The function should check whether the array length is zero and return a graceful false result before computing the last index.
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 function that removes an item from a list. Before removing, it calculates the position of the last item by subtracting one from the list's length. If the list is empty, that subtraction goes below zero, which the system does not allow, so the function crashes with an error. The fix is to check for an empty list first and return a clear message instead of crashing.
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.