Skip to content

Commit

Permalink
Merge pull request #5695 from multiversx/check-async-args-no-panic
Browse files Browse the repository at this point in the history
Check Async Args no panic
  • Loading branch information
gabi-vuls authored Nov 15, 2023
2 parents 5afa018 + e1278de commit adbd73a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions process/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,6 @@ var ErrNilManagedPeersHolder = errors.New("nil managed peers holder")

// ErrNilStorageService signals that a nil storage service has been provided
var ErrNilStorageService = errors.New("nil storage service")

// ErrInvalidAsyncArguments signals that invalid arguments were given for async/callBack processing
var ErrInvalidAsyncArguments = errors.New("invalid arguments to process async/callback function")
30 changes: 30 additions & 0 deletions process/smartContract/processorV2/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,36 @@ func TestScProcessor_CreateVMDeployBadCode(t *testing.T) {
require.Equal(t, badCodeError, err)
}

func TestScProcessor_CreateVMCallInputBadAsync(t *testing.T) {
t.Parallel()

vm := &mock.VMContainerMock{}
argParser := &mock.ArgumentParserMock{}
arguments := createMockSmartContractProcessorArguments()
arguments.VmContainer = vm
arguments.ArgsParser = argParser
sc, err := NewSmartContractProcessorV2(arguments)
require.NotNil(t, sc)
require.Nil(t, err)

tx := &smartContractResult.SmartContractResult{}
tx.Nonce = 0
tx.SndAddr = []byte("SRC")
tx.RcvAddr = []byte("DST")
tx.Data = []byte("data")
tx.Value = big.NewInt(45)
tx.CallType = vmData.AsynchronousCall

input, err := sc.createVMCallInput(tx, []byte{}, false)
require.Nil(t, input)
require.Equal(t, err, process.ErrInvalidAsyncArguments)

tx.CallType = vmData.AsynchronousCallBack
input, err = sc.createVMCallInput(tx, []byte{}, false)
require.Nil(t, input)
require.Equal(t, err, process.ErrInvalidAsyncArguments)
}

func TestScProcessor_CreateVMDeployInput(t *testing.T) {
t.Parallel()

Expand Down
15 changes: 11 additions & 4 deletions process/smartContract/processorV2/vmInputV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func (sc *scProcessor) createVMCallInput(

finalArguments, gasLocked := getAsyncCallGasLockFromTxData(callType, arguments)

asyncArguments, callArguments := separateAsyncArguments(callType, finalArguments)
asyncArguments, callArguments, err := separateAsyncArguments(callType, finalArguments)
if err != nil {
return nil, err
}

vmCallInput := &vmcommon.ContractCallInput{}
vmCallInput.VMInput = vmcommon.VMInput{}
Expand Down Expand Up @@ -150,9 +153,9 @@ func getAsyncCallGasLockFromTxData(callType vm.CallType, arguments [][]byte) ([]
return argsWithoutGasLocked, gasLocked
}

func separateAsyncArguments(callType vm.CallType, arguments [][]byte) ([][]byte, [][]byte) {
func separateAsyncArguments(callType vm.CallType, arguments [][]byte) ([][]byte, [][]byte, error) {
if callType == vm.DirectCall || callType == vm.ESDTTransferAndExecute {
return nil, arguments
return nil, arguments, nil
}

var noOfAsyncArguments int
Expand All @@ -162,14 +165,18 @@ func separateAsyncArguments(callType vm.CallType, arguments [][]byte) ([][]byte,
noOfAsyncArguments = 4
}

if len(arguments) < noOfAsyncArguments {
return nil, nil, process.ErrInvalidAsyncArguments
}

noOfCallArguments := len(arguments) - noOfAsyncArguments
asyncArguments := make([][]byte, noOfAsyncArguments)
callArguments := make([][]byte, noOfCallArguments)

copy(callArguments, arguments[:noOfCallArguments])
copy(asyncArguments, arguments[noOfCallArguments:])

return asyncArguments, callArguments
return asyncArguments, callArguments, nil
}

func buildAsyncArgumentsObject(callType vm.CallType, asyncArguments [][]byte) *vmcommon.AsyncArguments {
Expand Down

0 comments on commit adbd73a

Please sign in to comment.