Skip to content

Commit 1588744

Browse files
fix(evm-precompiles): add assertNumArgs validation (#2060)
1 parent 629aea3 commit 1588744

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
125125
- [#2045](https://github.com/NibiruChain/nibiru/pull/2045) - test(evm): backend tests with test network and real txs
126126
- [#2053](https://github.com/NibiruChain/nibiru/pull/2053) - refactor(evm): converted untyped event to typed and cleaned up
127127
- [#2054](https://github.com/NibiruChain/nibiru/pull/2054) - feat(evm-precompile): Precompile for one-way EVM calls to invoke/execute Wasm contracts.
128+
- [#2060](https://github.com/NibiruChain/nibiru/pull/2060) - fix(evm-precompiles): add assertNumArgs validation
128129

129130
#### Dapp modules: perp, spot, oracle, etc
130131

x/evm/precompile/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ func assertContractQuery(contract *vm.Contract) error {
4343

4444
return nil
4545
}
46+
47+
// assertNumArgs checks if the number of provided arguments matches the expected
48+
// count. If lenArgs does not equal wantArgsLen, it returns an error describing
49+
// the mismatch between expected and actual argument counts.
50+
func assertNumArgs(lenArgs, wantArgsLen int) error {
51+
if lenArgs != wantArgsLen {
52+
return fmt.Errorf("expected %d arguments but got %d", wantArgsLen, lenArgs)
53+
}
54+
return nil
55+
}

x/evm/precompile/funtoken.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ func (p precompileFunToken) decomposeBankSendArgs(args []any) (
205205
to string,
206206
err error,
207207
) {
208-
// Note: The number of arguments is valiated before this function is called
209-
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
210-
// which validates against the the structure of the precompile's ABI.
208+
if e := assertNumArgs(len(args), 3); e != nil {
209+
err = e
210+
return
211+
}
211212

212213
erc20, ok := args[0].(gethcommon.Address)
213214
if !ok {

x/evm/precompile/wasm.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,10 @@ func (p precompileWasm) queryRaw(
355355
return bz, err
356356
}
357357

358-
// Note: The number of arguments is valiated before this function is called
359-
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
360-
// which validates against the the structure of the precompile's ABI.
358+
if e := assertNumArgs(len(args), 2); e != nil {
359+
err = e
360+
return
361+
}
361362

362363
argIdx := 0
363364
wasmContract, e := parseContractAddrArg(args[argIdx])

x/evm/precompile/wasm_parse.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ func (p precompileWasm) parseInstantiateArgs(args []any, sender string) (
8484
txMsg wasm.MsgInstantiateContract,
8585
err error,
8686
) {
87-
// Note: The number of arguments is valiated before this function is called
88-
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
89-
// which validates against the the structure of the precompile's ABI.
87+
if e := assertNumArgs(len(args), 5); e != nil {
88+
err = e
89+
return
90+
}
9091

9192
argIdx := 0
9293
admin, ok := args[argIdx].(string)
@@ -142,9 +143,10 @@ func (p precompileWasm) parseExecuteArgs(args []any) (
142143
funds sdk.Coins,
143144
err error,
144145
) {
145-
// Note: The number of arguments is valiated before this function is called
146-
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
147-
// which validates against the the structure of the precompile's ABI.
146+
if e := assertNumArgs(len(args), 3); e != nil {
147+
err = e
148+
return
149+
}
148150

149151
argIdx := 0
150152
contractAddrStr, ok := args[argIdx].(string)
@@ -187,9 +189,10 @@ func (p precompileWasm) parseQueryArgs(args []any) (
187189
req wasm.RawContractMessage,
188190
err error,
189191
) {
190-
// Note: The number of arguments is valiated before this function is called
191-
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
192-
// which validates against the the structure of the precompile's ABI.
192+
if e := assertNumArgs(len(args), 2); e != nil {
193+
err = e
194+
return
195+
}
193196

194197
argsIdx := 0
195198
wasmContract, e := parseContractAddrArg(args[argsIdx])
@@ -220,9 +223,10 @@ func (p precompileWasm) parseExecuteMultiArgs(args []any) (
220223
},
221224
err error,
222225
) {
223-
// Note: The number of arguments is valiated before this function is called
224-
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
225-
// which validates against the the structure of the precompile's ABI.
226+
if e := assertNumArgs(len(args), 1); e != nil {
227+
err = e
228+
return
229+
}
226230

227231
arg := args[0]
228232
execMsgs, ok := arg.([]struct {

0 commit comments

Comments
 (0)