Integer Underflow in UInt64SetLib.py
The remove_item function in a smart contract library subtracts one from the array length without first checking that the array is non-empty. When called on an empty array, the subtraction underflows and the function reverts, which could cause a denial of service for dependent operations.
- Disclosed after under an hour
#49690 [SC-Low] Integer Underflow in UInt64SetLib.py
Submitted on Jul 18th 2025 at 12:17:53 UTC by @Opzteam for Audit Comp | Folks Smart Contract Library
- Report ID: #49690
- 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
A critical integer underflow vulnerability exists in the remove_item function of the UInt64SetLib.py smart contract library. The vulnerability allows the function to revert unexpectedly when operating on empty arrays, potentially causing denial of service conditions.
The remove_item function performs an unchecked subtraction operation without validating that the array contains at least one element. When items.length equals 0, the calculation items.length - 1 results in an underflow, producing a negative value that causes the function to revert.
@subroutine
def remove_item(to_remove: UInt64, items: DynamicArray[ARC4UInt64]) -> Tuple[Bool,
DynamicArray[ARC4UInt64]]:
last_idx = items.length - 1 # ← VULNERABLE LINE
for idx, item in uenumerate(items):
if item.native == to_remove:
last_item = items.pop()
if idx != last_idx:
items[idx] = last_item
return Bool(True), items.copy()
return Bool(False), items.copy()
Proof of Concept
Proof of Concept
- Attacker calls remove_item with any to_remove value on an empty array
- The function calculates last_idx = 0 - 1 = -1
- The underflow causes the function to revert
- Any dependent operations fail, potentially causing system-wide disruption
Recommended Fix
Add a length validation check before performing the subtraction:
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 computes the last index as length minus one without first checking that the array is non-empty, so an empty array causes an underflow and a revert.
Impact
Calling the function on an empty array reverts, which could cause a denial of service for any operation that depends on it.
What the attacker needed
Ability to call remove_item on an empty array
The fix
The function should check that the array has at least one element before subtracting one from its length.
How it was found
- Call remove_item with any value to remove on an empty array
- The function computes the last index as zero minus one
- The underflow makes the function revert
- Operations that depend on it then fail
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 removes an item from a list, but it first works out the position of the last item by subtracting one from the list's size. If the list is empty, that size is zero, and subtracting one gives a number the program cannot represent, so it crashes (reverts). Anything that relies on this function then fails too.
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.