Skip to content

Commit 37abd47

Browse files
authored
Parse extractAsUint256 values as unsigned decimal integers (#4894)
`extractAsUint256` on the JSON precompile parsed its value with `big.Int.SetString`, which takes a wider input grammar than the unsigned `uint256` the method returns. `ExtractAsUint256` now accepts only a plain string of decimal digits, quoted or unquoted, and returns an error for any other form. Gas and the 100-character length cap are unchanged. Inputs outside that form now revert where they previously returned a value, so this is app-hash-breaking. Covered by the extended `TestExtractAsUint256`.
1 parent 90b7acf commit 37abd47

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎precompiles/json/json.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ func (p PrecompileExecutor) ExtractAsUint256(_ sdk.Context, _ *abi.Method, args
192192

193193
// Convert the string to big.Int
194194
value, success := new(big.Int).SetString(strValue, 10)
195-
if !success {
196-
return nil, fmt.Errorf("failed to convert %s to big.Int", strValue)
195+
if !success || strings.TrimLeft(strValue, "0123456789") != "" {
196+
return nil, fmt.Errorf("failed to parse %s as an unsigned decimal integer", strValue)
197197
}
198198

199199
return value, nil

‎precompiles/json/json_test.go‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,25 @@ func TestExtractAsUint256(t *testing.T) {
104104
for _, test := range []struct {
105105
body []byte
106106
expectedOutput *big.Int
107+
wantErr bool
107108
}{
108-
{
109-
[]byte("{\"key\":\"12345678901234567890\"}"),
110-
n,
111-
}, {
112-
[]byte("{\"key\":\"0\"}"),
113-
big.NewInt(0),
114-
},
109+
{body: []byte(`{"key":"12345678901234567890"}`), expectedOutput: n},
110+
{body: []byte(`{"key":"0"}`), expectedOutput: big.NewInt(0)},
111+
{body: []byte(`{"key":5}`), expectedOutput: big.NewInt(5)},
112+
{body: []byte(`{"key":"-1"}`), wantErr: true},
113+
{body: []byte(`{"key":-5}`), wantErr: true},
114+
{body: []byte(`{"key":"+5"}`), wantErr: true},
115+
{body: []byte(`{"key":"-0"}`), wantErr: true},
115116
} {
116117
args, err := method.Inputs.Pack(test.body, "key")
117118
require.Nil(t, err)
118119
input := append(p.GetExecutor().(*json.PrecompileExecutor).ExtractAsUint256ID, args...)
119120
res, _, err := p.RunAndCalculateGas(evm, common.Address{}, common.Address{}, input, math.MaxUint64, nil, nil, true, false)
121+
if test.wantErr {
122+
require.Error(t, err, string(test.body))
123+
require.Nil(t, res)
124+
continue
125+
}
120126
require.Nil(t, err)
121127
output, err := method.Outputs.Unpack(res)
122128
require.Nil(t, err)

0 commit comments

Comments
 (0)