From ca82fd2690b8620e411b9c2cdde7a345cb91f95a Mon Sep 17 00:00:00 2001 From: masih Date: Tue, 17 Nov 2026 09:24:12 +0000 Subject: [PATCH] Parse extractAsUint256 values as unsigned decimal integers --- precompiles/json/json.go | 4 ++-- precompiles/json/json_test.go | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/precompiles/json/json.go b/precompiles/json/json.go index b63c8f27e6..79f333e73c 100644 --- a/precompiles/json/json.go +++ b/precompiles/json/json.go @@ -192,8 +192,8 @@ func (p PrecompileExecutor) ExtractAsUint256(_ sdk.Context, _ *abi.Method, args // Convert the string to big.Int value, success := new(big.Int).SetString(strValue, 10) - if !success { - return nil, fmt.Errorf("failed to convert %s to big.Int", strValue) + if !success || strings.TrimLeft(strValue, "0123456789") != "" { + return nil, fmt.Errorf("failed to parse %s as an unsigned decimal integer", strValue) } return value, nil diff --git a/precompiles/json/json_test.go b/precompiles/json/json_test.go index 31e98435aa..f0978589c4 100644 --- a/precompiles/json/json_test.go +++ b/precompiles/json/json_test.go @@ -104,19 +104,25 @@ func TestExtractAsUint256(t *testing.T) { for _, test := range []struct { body []byte expectedOutput *big.Int + wantErr bool }{ - { - []byte("{\"key\":\"12345678901234567890\"}"), - n, - }, { - []byte("{\"key\":\"0\"}"), - big.NewInt(0), - }, + {body: []byte(`{"key":"12345678901234567890"}`), expectedOutput: n}, + {body: []byte(`{"key":"0"}`), expectedOutput: big.NewInt(0)}, + {body: []byte(`{"key":5}`), expectedOutput: big.NewInt(5)}, + {body: []byte(`{"key":"-1"}`), wantErr: true}, + {body: []byte(`{"key":-5}`), wantErr: true}, + {body: []byte(`{"key":"+5"}`), wantErr: true}, + {body: []byte(`{"key":"-0"}`), wantErr: true}, } { args, err := method.Inputs.Pack(test.body, "key") require.Nil(t, err) input := append(p.GetExecutor().(*json.PrecompileExecutor).ExtractAsUint256ID, args...) res, _, err := p.RunAndCalculateGas(evm, common.Address{}, common.Address{}, input, math.MaxUint64, nil, nil, true, false) + if test.wantErr { + require.Error(t, err, string(test.body)) + require.Nil(t, res) + continue + } require.Nil(t, err) output, err := method.Outputs.Unpack(res) require.Nil(t, err)