Underflow Revert in remove_item When Removing from an Empty Array
The remove_item subroutine in a smart contract library subtracts one from the array length without first checking whether the array is empty, so the subtraction underflows. An attacker or any caller passing an empty array causes the transaction to abort instead of receiving the promised 'not present' result.
Originally disclosed on Immunefi
- Disclosed after under an hour
#49938 [SC-Low] Underflow Revert in `remove_item` When Removing from an Empty Array
Submitted on Jul 20th 2025 at 16:30:46 UTC by @Afriauditor for Audit Comp | Folks Smart Contract Library
- Report ID: #49938
- Report Type: Smart Contract
- Report severity: Low
- Target: https://github.com/Folks-Finance/algorand-smart-contract-library/blob/main/contracts/library/UInt64SetLib.py
- Impacts:
- Contract fails to deliver promised returns, but doesn't lose value
Description
Brief/Intro
The remove_item subroutine in UInt64SetLib unconditionally computes last_idx = items.length - 1, which underflows when the array is empty and causes the transaction to revert instead of returning (false, items), leading to unexpected failures or DoS in production.
Vulnerability Details
In the first line of remove_item the code does:
last_idx = items.length - 1
Because there is no check for an empty array before this subtraction, any call to remove_item(x, []) will immediately abort the TEAL execution at that line, bypassing the intended “not present” return path. So the function violates its specification and any upstream contract logic reverts
Impact Details
Because remove_item aborts instead of returning the promised (false, items) when called on an empty array, any contract logic that depends on that return value will itself revert or misbehave breaking workflows
References
Proof of Concept
Proof of Concept
- Deploy the
UInt64SetLibExposedapplication so you have an ABI methoddynamicRemoveItemthat callsremove_itemon a box-backed array. - Reset the box to an empty array by invoking its
dynamicResetmethod (or equivalent), ensuringitems.length == 0. - Attempt to remove any value (e.g.
123) by callingdynamicRemoveItem({ args: [123], boxReferences: ["uint64_set"] }). - Watch the transaction revert with an underflow or “invalid subtract” error at the very first line of
remove_item. - Confirm that no
(false, items)return is ever emitted and that the fallback return path is never reached.
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 as the array length minus one before checking whether the array is empty, so the subtraction underflows on an empty array.
Impact
Any contract logic relying on the promised false return when removing from an empty array will itself revert, which could break workflows or cause a denial of service.
What the attacker needed
Caller can invoke remove_item; array is empty
How it was found
- Deploy the exposed library application that calls remove_item on a box-backed array.
- Reset the box so the array is empty.
- Call the remove method with any value.
- Observe the transaction revert at the first line of remove_item.
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 is a program that runs on a blockchain. This library function is meant to remove a number from a list and politely report back that the number was not there if the list is empty. Instead, it does a subtraction that only works when the list has at least one item, so with an empty list the whole transaction crashes.
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.