No items length check in remove_item leads to a revert with an underflow
The remove_item function in a smart contract library does not check whether the items array is empty before subtracting one from its length. Calling it with an empty array causes an underflow panic and reverts the transaction.
- Disclosed after under an hour
#48885 [SC-Low] No items length check in remove_item leads to a revert with an underflow
Submitted on Jul 8th 2025 at 18:32:15 UTC by @j3x for Audit Comp | Folks Smart Contract Library
- Report ID: #48885
- Report Type: Smart Contract
- Report severity: Low
- Target: https://github.com/Folks-Finance/algorand-smart-contract-library/blob/main/contracts/library/UInt64SetLib.py
- Impacts:
Description
Brief/Intro
No items length check in remove_item leads to a revert with an underflow.
Vulnerability Details
In UInt64SetLib::remove_item(), the items array length is not verified to be a non-zero value, and the function will directly proceed to subtract from it:
def remove_item(to_remove: UInt64, items: DynamicArray[ARC4UInt64]) -> Tuple[Bool, DynamicArray[ARC4UInt64]]:
last_idx = items.length - 1
...
If the items array is empty, this will lead to a subtration from a 0 uint64, which causes a panic.
Impact Details
Contradicts with the system's best design, as it proceeds to directly subtracts from a length of 0, causing a revert with an underflow.
References
Proof of Concept
Proof of Concept
- The function
remove_item()is called with an emptyitemsarray. - This will lead to an underflow, causing a panic, which reverts the transaction
Mitigation
add the following lines before subtracting:
if items.length == 0:
return Bool(False), items.copy()
This will return peacefully in case on an empty set.
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 function subtracts one from the array length without first checking that the array is non-empty.
Impact
The transaction reverts with an underflow, which contradicts the system's best design; the text does not claim any loss of funds or other damage.
What the attacker needed
None
The fix
The function should check whether the array length is zero and return early before subtracting.
How it was found
- Call remove_item with an empty items array.
- The function subtracts one from the zero length, causing an underflow panic that reverts the transaction.
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 function tries to remove an item from a list. It first calculates the last position by subtracting one from the list's length. If the list is empty, that subtraction goes below zero, which the system cannot handle, so the whole transaction fails and is undone.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.