Skip to content

Commit

Permalink
fix panic when block tag greater than MaxInt64 is provided in parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nddeluca committed May 2, 2024
1 parent c6a34b0 commit 79b1cad
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
23 changes: 16 additions & 7 deletions decode/evm_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,24 @@ func ParseBlockNumberFromParams(methodName string, params []interface{}) (int64,
}

blockNumber, exists := BlockTagToNumberCodec[tag]
if exists {
return blockNumber, nil
}

if !exists {
spaceint, valid := cosmosmath.NewIntFromString(tag)
if !valid {
return 0, fmt.Errorf(fmt.Sprintf("unable to parse tag %s to integer", tag))
}
return blockParamToInt64(tag)
}

// blockParamToInt64 converts a 0x prefixed base 16 or no-prefixed base 10 string to int64
// and returns an error if value is unable to be converted or out of bounds
func blockParamToInt64(blockParam string) (int64, error) {
result, valid := cosmosmath.NewIntFromString(blockParam)
if !valid {
return 0, fmt.Errorf("unable to parse tag %s to integer", blockParam)
}

blockNumber = spaceint.Int64()
if !result.IsInt64() {
return 0, fmt.Errorf("value %s out of range", blockParam)
}

return blockNumber, nil
return result.Int64(), nil
}
22 changes: 22 additions & 0 deletions decode/evm_rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ func TestUnitTest_ParseBlockNumberFromParams(t *testing.T) {
expectedBlockNumber: 0,
expectedErr: "error decoding block number param from params",
},
{
name: "errors on base10 int64 overflow",
req: EVMRPCRequestEnvelope{
Method: "eth_getBlockByNumber",
Params: []interface{}{
"9223372036854775808", false,
},
},
expectedBlockNumber: 0,
expectedErr: "out of range",
},
{
name: "errors on base16 int64 overflow",
req: EVMRPCRequestEnvelope{
Method: "eth_getBlockByNumber",
Params: []interface{}{
"0x8000000000000000", false,
},
},
expectedBlockNumber: 0,
expectedErr: "out of range",
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 79b1cad

Please sign in to comment.