Skip to content

Commit

Permalink
accounts/abi: prefer new(big.Int) over big.NewInt(0) (ethereum#25087)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Jan 10, 2025
1 parent 16f1b0a commit b3127f5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions accounts/abi/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {

// lengthPrefixPointsTo interprets a 32 byte slice as an offset and then determines which indices to look to decode the type.
func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) {
bigOffsetEnd := big.NewInt(0).SetBytes(output[index : index+32])
bigOffsetEnd := new(big.Int).SetBytes(output[index : index+32])
bigOffsetEnd.Add(bigOffsetEnd, common.Big32)
outputLength := big.NewInt(int64(len(output)))

Expand All @@ -269,11 +269,9 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err
}

offsetEnd := int(bigOffsetEnd.Uint64())
lengthBig := big.NewInt(0).SetBytes(output[offsetEnd-32 : offsetEnd])
lengthBig := new(big.Int).SetBytes(output[offsetEnd-32 : offsetEnd])

totalSize := big.NewInt(0)
totalSize.Add(totalSize, bigOffsetEnd)
totalSize.Add(totalSize, lengthBig)
totalSize := new(big.Int).Add(bigOffsetEnd, lengthBig)
if totalSize.BitLen() > 63 {
return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize)
}
Expand All @@ -288,7 +286,7 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err

// tuplePointsTo resolves the location reference for dynamic tuple.
func tuplePointsTo(index int, output []byte) (start int, err error) {
offset := big.NewInt(0).SetBytes(output[index : index+32])
offset := new(big.Int).SetBytes(output[index : index+32])
outputLen := big.NewInt(int64(len(output)))

if offset.Cmp(outputLen) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestMultiReturnWithStringArray(t *testing.T) {
}
buff := new(bytes.Buffer)
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
temp, _ := big.NewInt(0).SetString("30000000000000000000", 10)
temp, _ := new(big.Int).SetString("30000000000000000000", 10)
ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
Expand Down

0 comments on commit b3127f5

Please sign in to comment.