Skip to content
Smart contracts Audit finding Intermediate

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.

hunter0xweb3 Follow

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

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

Python
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

https://github.com/Folks-Finance/algorand-smart-contract-library/blob/7673a43fa5183af736b65f17d1a297fdea672059/contracts/library/UInt64SetLib.py#L32-L41

Proof of Concept

Proof of Concept

Create the following test case in
tests/library/UInt64SetLib.test.ts inside remove item section:

TS
    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

Bash
  ● 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

The original report as published on Immunefi, by hunter0xweb3. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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

  1. Write a test that calls removeItem with an empty items array.
  2. 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.

More by hunter0xweb3

View all

8 more disclosed reports, best first.

More from Folks Smart Contract Library

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.