Skip to content
DoS Audit finding Beginner

The remove functionality in UInt64SetLib::remove_item underflows on empty array

The remove_item function in UInt64SetLib computes the last index as items.length - 1 without checking for an empty array. Calling it with an empty array causes an unsigned underflow and aborts the transaction, reverting the call.

NHristov Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#49559 [SC-Low] The remove functionality in `UInt64SetLib::remove_item` underflows on empty array

Submitted on Jul 17th 2025 at 08:37:56 UTC by @NHristov for Audit Comp | Folks Smart Contract Library

Description

Brief/Intro

The UInt64SetLib::remove_item computes the last index as

Python
last_idx = items.length - 1

without first checking if the passed array is empty. If you call remove_item with an empty array, items.length - 1 underflows to a negative value and immediately aborts the TEAL execution, reverting the transaction.

Vulnerability Details

In contracts/library/UInt64SetLib.py we have:

Python
# filepath: contracts/library/UInt64SetLib.py
@subroutine
def remove_item(
    to_remove: UInt64,
    items: DynamicArray[ARC4UInt64]
) -> Tuple[Bool, DynamicArray[ARC4UInt64]]:
    # ← no guard for empty `items`
    last_idx = items.length - 1
    for idx, item in uenumerate(items):
        if item.native == to_remove.native:
            last_item = items.pop()
            if idx != last_idx:
                items[idx] = last_item
            return Bool(True), items.copy()
    return Bool(False), items.copy()

and if we pass an empty array, items.length is 0, so last_idx becomes -1. This leads to an immediate TEAL abort with the error message copied from the PoC test below as follows:

frame_dig -1

    intc_0 // 0
    extract_uint16
    dupn 2
    intc_1 // 1
    - <--- Error
    swap
    // contracts/library/UInt64SetLib.py:34
    // if items.length == 0:
    bnz remove_item_after_if_else@2

That “frame dig ‑1” error is coming from this line:

Python
last_idx = items.length - 1

When items.length is 0, you end up emitting TEAL that does:

TEAL
// push items.length (0)
extract_uint16
dup
intc 1
-
swap
…

Which effectively computes 0 - 1 in unsigned land, underflows, and blows up with “frame dig ‑1”.

Impact Details

Any consumer of remove_item that passes an empty array will experience an immediate revert. This can lead to:

  • Unexpected Denial-of-Service in higher-level logic that relies on safe removal.
  • Forcing callers to add additional pre-checks, undermining the library’s usability guarantees.

Remediation

Add an explicit guard against an empty array at the top of remove_item, for example:

Python
if items.length == 0:
    return Bool(False), items.copy()
last_idx = items.length - 1

References

Proof of Concept

Proof of Concept

In tests/library/UInt64SetLib.test.ts append the following test under the describe("remove item") block:

TypeScript
// filepath: tests/library/UInt64SetLib.test.ts
describe("remove item", () => {
  // …

  test("fails when removing from empty array", async () => {
    // calling removeItem on [] should revert
    await expect(
      client.removeItem({ args: [34n, []] })
    ).toThrow();
  });
});

The original report as published on Immunefi, by NHristov. 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

The function calculates last_idx = items.length - 1 without first checking whether the array is empty, leading to an unsigned integer underflow.

Impact

Any caller passing an empty array will experience an immediate revert, potentially causing denial of service in higher-level logic that relies on safe removal.

What the attacker needed

Caller passes an empty array

The fix

Add an explicit guard at the top of remove_item to return false and the unchanged array if items.length is zero.

How it was found

  1. Call remove_item with an empty array.
  2. Observe the transaction reverts due to underflow.

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 function that removes an item from a list. If the list is empty, the code tries to calculate a position that becomes negative, which the system cannot handle, so the whole transaction fails. This means any program using this function must check for an empty list first, or it will crash.

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.

More by NHristov

View all

6 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.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.