Skip to content
Smart contracts Audit finding Intermediate

UniswapV3’s path issue for swapExactOutput

The `_reverseBytes` function reverses the entire encoded path byte-by-byte, which corrupts token addresses and breaks multi-hop swaps in `swapExactOutput`. As a result, the function cannot be used for multi-hop swaps.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

UniswapV3 expects a path object like (tokenA, feeAB, tokenB, feeBC, tokenC). The UniV3Trader.swapExactOutput code tries to reverse this path to get to (tokenC, feeBC, tokenB, feeAB, tokenA) but that’s not what the _reverseBytes function does. Note that it reverts the entire encoded path byte array byte-by-byte which breaks the byte-order in a token. For example, tokenA would have every single byte reversed and lead to a different token.

function _reverseBytes(bytes memory input) internal pure returns (bytes memory output) {
    /** @audit reverses byte order? */
    for (uint256 i = 0; i < input.length; ++i) output[i] = input[input.length - 1 - i];
}

Impact

The UniV3Trader.swapExactOutput function with multi-hops is broken and cannot be used.

Recommended Mitigation Steps

Don’t reverse the path byte-by-byte but element-by-element.

0xleastwood (judge) commented:

Can you confirm if this issue is valid or not? @MihanixA

0xleastwood (judge) commented:

I actually agree with the warden’s finding here. Leaving as is.

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

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 `_reverseBytes` function reverses the entire byte array instead of reversing the path elements (tokens and fees) individually.

What the attacker needed

None

The fix

The path should be reversed element-by-element, not byte-by-byte.

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.

In a multi-hop swap, tokens are swapped through several intermediate tokens. The code needs to reverse the order of the tokens in the path, but it reverses the bytes of the whole path instead, which scrambles the token addresses. This makes the function unusable for multi-hop swaps.

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 cmichel

View all

196 more disclosed reports, best first.

More from Mellow Protocol contest

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.