Invariant breakage , Edge case Integer UInt64SetLib.py::remove_item leads to int underflow
The remove_item function in a smart contract library computes an index as the array length minus one without checking for an empty array, so passing an empty set causes an integer underflow and the call reverts. An attacker or caller could trigger this revert, breaking the promised behaviour that removing from a set returns a set.
Originally disclosed on Immunefi
- Disclosed after under an hour
#49527 [SC-Low] Edge case Integer UInt64SetLib.py::remove_item leads to int underflow
Submitted on Jul 16th 2025 at 21:18:01 UTC by @hunter0xweb3 for Audit Comp | Folks Smart Contract Library
- Report ID: #49527
- 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
UInt64SetLib.py::remove_item doesnt checks that items parameter is not empty, leading to int underflow when an empty items set is used as a parameter
removing an element from an empty set should return an empty set , it should not revert, this breaks the promised return of calling remove_item from a set returns a set
Vulnerability Details
UInt64SetLib.py::remove_item edge case leads to int underflow when calling remove_item with an empty set.
The vulnerability occurs because function declares the variable last_idx to be equal to array length - 1:
https://github.com/Folks-Finance/algorand-smart-contract-library/blob/7673a43fa5183af736b65f17d1a297fdea672059/contracts/library/UInt64SetLib.py#L32-L41
def remove_item(to_remove: UInt64, items: DynamicArray[ARC4UInt64]) -> Tuple[Bool, DynamicArray[ARC4UInt64]]:
@> last_idx = items.length - 1
However if empty array is passed this leads to a frame dig -1 and call to be reverted, instead of returning an empty array
This is significative because call parameters to this functions are dynamic and no data integrity assumptions should be made, so defense in depth should be enforced
Impact Details
- Function remove_item doesnt applies security in depth mechanism
- remove_item is integrity inconsistent, removing an element from an empty set should return an empty set , it should not revert, this breaks the promised return of calling remove_item from a set returns a set
References
Proof of Concept
Proof of Concept
Create the following test case in
tests/library/UInt64SetLib.test.ts inside remove item section:
test("Removing from empty arrays reverts instead of returning empty array ", async () => {
const items = [];
expect(await client.removeItem({ args: [0n, items] })).toEqual([false, items]);
});
Observe function remove_item reverts but it should return an empty array
● UInt64SetLib › remove item › Removing from empty arrays reverts instead of returning empty array
frame_dig -1
intc_0 // 0
extract_uint16
dup
intc_1 // 1
- <--- Error
intc_0 // 0
remove_item_for_header@1:
// contracts/library/UInt64SetLib.py:34
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 last_idx as items.length - 1 without first checking that the array is non-empty, so an empty array underflows the unsigned integer.
Impact
Calling remove_item with an empty set reverts rather than returning an empty set, so the function fails to deliver its promised return; no loss of value is claimed.
What the attacker needed
Caller passes an empty set
The fix
The function should check whether the array is empty and return an empty result before computing the last index.
How it was found
- Write a test that calls removeItem with an empty items array.
- Run the test and observe the call reverts with a frame_dig -1 error instead of returning an empty array.
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 an item from a list. It works out the last position by subtracting one from the list's length, but if the list is empty the length is zero, so it tries to use minus one as a position, which is invalid and makes the call fail. The author argues it should simply return the empty list instead of failing.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.