foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.28k stars 1.75k forks source link

Decoding bytes resulting from `encodePacked` causes a revert REPL contract #9264

Closed JacoboLansac closed 2 hours ago

JacoboLansac commented 3 hours ago

Component

Chisel

Have you ensured that all of these are up to date?

What version of Foundry are you on?

forge 0.2.0 (c3069a5 2024-11-05T00:22:10.424475717Z)

What command(s) is the bug in?

abi.decode() after calling abi.encodePacked()

Operating System

Linux

Describe the bug

> address addr1 = 0x1234567890123456789012345678901234567890;
> uint256 value1 = 100000;
> bytes memory packed1 = abi.encodePacked(addr1, value1);

// the following line reverts
> (address decodedAdd, uint256 decodedValue) = abi.decode(packed1, (address,uint256));

The revert looks like this:

➜ (address decodedAdd, uint256 decodedValue) = abi.decode(packed1, (address,uint256));
Traces:
  [804] 0xBd770416a3345F91E4B34576cb804a576fa48EB1::run()
    └─ ← [Revert] EvmError: Revert

Error: Failed to execute REPL contract!
➜ 
klkvr commented 2 hours ago

encodePacked does not pad address with 0 bytes producing invalid abi encoding which can't be decoded by abi.encode

abi.encode(addr1, value1):

0x000000000000000000000000123456789012345678901234567890123456789000000000000000000000000000000000000000000000000000000000000186a0

abi.encodePacked(addr1, value1):

0x123456789012345678901234567890123456789000000000000000000000000000000000000000000000000000000000000186a0000000000000000000000000