Skip to content
Smart contracts Audit finding Intermediate

Unchecked Underflow in TreeTickLib Causes Transaction Revert in Route Creation

An unchecked cast in TreeTickLib converts negative tick values into large positive tree indices, so the computed low index ends up greater than the high index. Users calling the modify function to update pool positions then hit a range validation revert, blocking liquidity operations.

0xapple Follow with Edoscoba and TheSavageTeddy

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-09-ammplify-judging/issues/261

Found by

0xapple, Edoscoba, TheSavageTeddy

Summary

Unchecked arithmetic underflow in TreeTickLib.sol causes a transaction revert for users attempting to modify pool positions, as the system generates an invalid tree index that triggers a failure in the RouteImpl.make function's range validation.

Root Cause

In TreeTickLib.sol within the tickToTreeIndex function, the calculation uint24(tick / tickSpacing) + rootWidth / 2 is performed inside an unchecked block. When tick / tickSpacing is sufficiently negative (e.g., less than -rootWidth / 2), the cast uint24(tick / tickSpacing) converts the negative result to a large positive value due to underflow (e.g., -524,289 becomes 16,252,927). Adding rootWidth / 2 to this does not cause another underflow but rather produces an incorrect tree index (e.g., 16,515,071), which propagates through the system, leading to a revert in downstream logic.

Solidity
function tickToTreeIndex(int24 tick, uint24 rootWidth, int24 tickSpacing) internal pure returns (uint24) {
    unchecked {
        require(tick % tickSpacing == 0, UnalignedTick(tick, tickSpacing));
        return uint24(tick / tickSpacing) + rootWidth / 2; // Underflow occurs here if result is negative
    }
}

Internal Pre-conditions

None

External Pre-conditions

None

Attack Path

  1. A user initiates a pool position update by calling WalkerLib.modify with a valid negative lowTick (e.g., -524,289), a valid highTick (e.g., -200,000), tickSpacing = 1, and rootWidth = 524,288 (as computed by calcRootWidth).

    Solidity
    function modify(PoolInfo memory pInfo, int24 lowTick, int24 highTick, Data memory data) internal {
        uint24 low = pInfo.treeTick(lowTick); // Calls treeTick with lowTick
        uint24 high = pInfo.treeTick(highTick) - 1; // Calls treeTick with highTick
        Route memory route = RouteImpl.make(pInfo.treeWidth, low, high);
        route.walk(down, up, phase, toRaw(data));
    }
    
  2. The modify function calls PoolInfoImpl.treeTick for both ticks, which invokes TreeTickLib.tickToTreeIndex to convert each tick to a uint24 tree index.

    Solidity
    function treeTick(PoolInfo memory self, int24 tick) internal pure returns (uint24) {
        return TreeTickLib.tickToTreeIndex(tick, self.treeWidth, self.tickSpacing);
    }
    
  3. For lowTick = -524,289, tickToTreeIndex computes tick / tickSpacing = -524,289 / 1 = -524,289. The cast uint24(-524,289) underflows to 2^24 - 524,289 = 16,252,927. Adding rootWidth / 2 = 524,288 / 2 = 262,144 yields 16,252,927 + 262,144 = 16,515,071.

  4. For highTick = -200,000, tickToTreeIndex computes tick / tickSpacing = -200,000 / 1 = -200,000. The cast uint24(-200,000) underflows to 2^24 - 200,000 = 16,577,216. Adding rootWidth / 2 = 524,288 / 2 = 262,144 yields 16,577,216 + 262,144 = 16,839,360. As this value is bigger than uint24 can fit it yields 62,144

  5. The modify function passes these indices to RouteImpl.make(pInfo.treeWidth, low, high), where low = 16,515,071 and high = 224,288. The make function checks _right < _left:

    Solidity
    function make(uint24 _rootWidth, uint24 _left, uint24 _right) internal pure returns (Route memory) {
        if (_right < _left) revert InvertedRange(_left, _right); // Reverts here
    }
    

    Since 224,288 < 16,515,071, the transaction reverts with an InvertedRange error.

Impact

Users cannot successfully execute the modify function to update pool positions, as the transaction reverts due to the invalid tree index causing a range validation failure in RouteImpl.make. This prevents users from performing intended operations on the pool, such as adding liquidity positions. This effectively disables the core functionality of the protocol.

PoC

No response

Mitigation

No response

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/itos-finance/Ammplify/pull/25

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

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 tick-to-index conversion casts a signed division result to an unsigned integer inside an unchecked block, so negative ticks wrap to large positive indices instead of being handled or rejected.

Impact

Users cannot execute modify to update pool positions, which prevents adding liquidity and effectively disables core protocol functionality.

What the attacker needed

None

The fix

The protocol team fixed this issue in a pull request, presumably by handling negative tick results correctly instead of letting them wrap.

How it was found

  1. A user calls WalkerLib.modify with a valid negative low tick, a valid high tick, a tick spacing of one and a root width from calcRootWidth.
  2. modify calls PoolInfoImpl.treeTick for both ticks, which calls TreeTickLib.tickToTreeIndex.
  3. For the negative low tick, the signed division result is cast to uint24 and wraps to a large positive value, then the half root width is added.
  4. For the high tick, the wrapped value plus the half root width overflows past the uint24 maximum and wraps again to a small value.
  5. modify passes both indices to RouteImpl.make, which sees the right index below the left index and reverts with InvertedRange.

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 tick is a price step used by the pool, and the code stores it in a number type that cannot hold negatives. When a negative tick is squeezed into that type it wraps around to a huge positive number. The pool then thinks the lower end of a range sits above the upper end and refuses the whole transaction.

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 0xapple

View all

3 more disclosed reports, best first.

More from Ammplify

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.