UInt64SetLib#remove_item would revert if the item is empty
The remove_item function in a smart contract library computes an index from the array length minus one without handling an empty array, so the subtraction produces a negative value the virtual machine rejects. An attacker or caller passing an empty set would cause the function to revert instead of returning false.
- Disclosed after under an hour
#49390 [SC-Low] `UInt64SetLib#remove_item` would revert if the item is empty
Submitted on Jul 15th 2025 at 12:24:21 UTC by @pks271 for Audit Comp | Folks Smart Contract Library
- Report ID: #49390
- 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
Bug Description
When items is empty, the last_idx = items.length - 1 = -1 will revert because the vm seems dont support negative value, but the remove_item function don't handle such case.
Impact
The remove_item function would revert instead of return false if the item is empty.
Recommendation
Check the item is empty or not before last_idx.
Proof of Concept
Proof of Concept
Insert the case into tests/library/UInt64SetLib.test.ts:
test("removes from empty array", async () => {
expect(await client.removeItem({ args: [873099n, []] })).toEqual([false, []]);
expect(await client.removeItem({ args: [34n, []] })).toEqual([false, []]);
});
Result:
● UInt64SetLib › remove item › removes from 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 subtracts one from the array length to get the last index without first checking whether the array is empty, producing a negative value the VM cannot represent.
Impact
The function reverts rather than returning false when the set is empty, so callers relying on a boolean result may fail; no value is lost.
What the attacker needed
Caller passes an empty set
The fix
The function should check whether the set is empty before computing the last index and return false in that case.
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 set and politely report whether it found it. When the set is empty, the code does a subtraction that goes below zero, which the blockchain runtime does not allow, so the whole call crashes instead of just saying 'not found'.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.