Skip to content
DoS Audit finding Beginner

Integer underflow in remove_item leads to AVM trap and DoS via empty array call

The remove_item function in a reusable UInt64 set library subtracts one from the array length without checking that the array is non-empty, so an empty array causes an integer underflow. Any contract calling it with an empty array hits a hard trap in the Algorand Virtual Machine, failing the whole transaction and allowing griefing or denial of service.

Bug82427 Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#48990 [SC-Low] Integer underflow in remove_item leads to AVM trap and DoS via empty array call

Submitted on Jul 10th 2025 at 09:20:14 UTC by @Bug82427 for Audit Comp | Folks Smart Contract Library

Description

Brief/Intro The remove_item function in the UInt64SetLib library is vulnerable to an integer underflow when called with an empty array. The line last_idx = items.length - 1 causes a wraparound on subtraction when the array is empty, resulting in a hard trap in the Algorand Virtual Machine (AVM). This causes the entire transaction to fail and introduces a vector for griefing or denial-of-service (DoS) when invoked without proper guards.

Vulnerability Details The vulnerability lies in this line:

python

last_idx = items.length - 1

This operation assumes that items.length is at least 1. However, if the input array is empty, this becomes 0 - 1, which underflows in the UInt64 domain and wraps to 2^64 - 1. This triggers a trap on AVM execution due to an invalid operation.

There is no input validation or precondition that ensures the array is non-empty. Thus, any contract that uses this library function without additional validation becomes vulnerable to a full halt in execution.

This violates both defensive programming practices and safe library behavior expected in reusable utility modules.

References CWE-191: Integer Underflow (Wrap or Wraparound) — https://cwe.mitre.org/data/definitions/191.html

Proof of Concept

Step-by-step PoC Instantiate an empty DynamicArray[ARC4UInt64]:

python

items = DynamicArrayARC4UInt64 Call the vulnerable function:

python remove_item(UInt64(123), items) Inside remove_item, the following line executes:

python

last_idx = items.length - 1 # 0 - 1 = underflow This results in:

python

last_idx = UInt64(2^64 - 1) AVM traps execution, crashing the transaction entirely. No error is recoverable.

This can be triggered even with benign input and becomes dangerous in multi-step contract flows, as it halts any downstream execution logic.

  1. Remediation Add a simple guard clause at the beginning of remove_item:

python

if items.length == 0: return Bool(False), items.copy() This prevents the invalid subtraction and ensures safe use even in edge cases. It also maintains consistent return types.

The original report as published on Immunefi, by Bug82427. 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 code computes the last index as length minus one without first checking that the array is non-empty, so an empty array underflows.

Impact

Any contract using the library without its own guard can have its execution halted, enabling griefing or denial of service with no economic damage beyond transaction fees.

What the attacker needed

A contract calls the library function; the array passed in is empty

The fix

The report recommends adding a guard at the start of remove_item that returns false and a copy of the array when the length is zero.

How it was found

  1. Create an empty dynamic array of unsigned 64-bit integers.
  2. Call remove_item with any value and that empty array.
  3. The function computes the last index as length minus one, which underflows.
  4. The virtual machine traps and the transaction fails.

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 library is shared code that many contracts reuse. This one removes a number from a list, and to find the last position it subtracts one from the list's length. If the list is empty, the length is zero, and subtracting one wraps around to a huge number instead of failing cleanly, which crashes the contract run.

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 Bug82427

View all

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