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.26k stars 1.74k forks source link

bug(`cast`): `2d` \ `2h` not returning consistent results (swallows leading 0) #7657

Open RonTuretzky opened 6 months ago

RonTuretzky commented 6 months ago

Component

Cast

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

What version of Foundry are you on?

forge 0.2.0 (f625d0f 2024-04-02T00:16:42.810315000Z)

What command(s) is the bug in?

cast 2d \ 2h

Operating System

macOS (Intel)

Describe the bug

cast 2d 0x09845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c returns 4304691653437447671144768329537717612774901428585623103913861666989519717164 however cast 2h 4304691653437447671144768329537717612774901428585623103913861666989519717164 returns 0x9845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c

However, these two outputs are not equivalent,as the first starts with 0x09 and the latter starts with 0x9

This behavior is also demonstrated in python

>>> a = 0x09845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c
>>> a
4304691653437447671144768329537717612774901428585623103913861666989519717164
>>> hex(a)
'0x9845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c'

This breaks expected behavior, for example , method 10 on this contract

(getOperatorFromId)

returns the expected value for 0x09845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c, and return null for 0x9845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c

This is also demonstrated when using cast

❯ cast c 0xc4A6E362e8Bd89F28Eb405F9Aa533784884B9c4F "function getOperatorFromId(bytes32)"  0x09845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c -r https://holesky.drpc.org
0x00000000000000000000000042bbfc9c9d78265a96a003dd858ed96fe7d757b7

❯ cast c 0xc4A6E362e8Bd89F28Eb405F9Aa533784884B9c4F "function getOperatorFromId(bytes32)"  0x9845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c -r https://holesky.drpc.org
Error: 
parser error:
0x9845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c
                                                                 ^
Odd number of digits

Context:
- parser error:
0x9845e7f93d27198144355dbac3f0cdc34d74f78f40c9a37f0969a924e04c32c
                                                                 ^
Odd number of digits
zerosnacks commented 1 week ago

Hi @RonTuretzky thanks for flagging

Able to reproduce with the given example, it indeed looks like leading 0's are swallowed which is incorrect.