[#807] morph/container: Add opcode check

Parsers return error if unexpected opcode
returns.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-09-09 14:41:04 +03:00 committed by Pavel Karpy
parent 6f41a979ca
commit ffde45d164
2 changed files with 20 additions and 3 deletions

View file

@ -50,20 +50,28 @@ var errUnexpectedArgumentAmount = fmt.Errorf("unexpected arguments amount in %s
// ParsePutNotary from NotaryEvent into container event structure. // ParsePutNotary from NotaryEvent into container event structure.
func ParsePutNotary(ne event.NotaryEvent) (event.Event, error) { func ParsePutNotary(ne event.NotaryEvent) (event.Event, error) {
var ev Put var (
ev Put
currentOp opcode.Opcode
)
fieldNum := 0 fieldNum := 0
for _, op := range ne.Params() { for _, op := range ne.Params() {
switch op.Code() { currentOp = op.Code()
case opcode.PUSHDATA1, opcode.PUSHDATA2, opcode.PUSHDATA4:
switch {
case opcode.PUSHDATA1 <= currentOp && currentOp <= opcode.PUSHDATA4:
if fieldNum == expectedItemNumPut { if fieldNum == expectedItemNumPut {
return nil, errUnexpectedArgumentAmount return nil, errUnexpectedArgumentAmount
} }
fieldSetters[fieldNum](&ev, op.Param()) fieldSetters[fieldNum](&ev, op.Param())
fieldNum++ fieldNum++
case opcode.PUSH0 <= currentOp && currentOp <= opcode.PUSH16 || currentOp == opcode.PACK:
// array packing opcodes. do nothing with it
default: default:
return nil, event.UnexpectedOpcode(PutNotaryEvent, op.Code())
} }
} }

View file

@ -1,8 +1,11 @@
package event package event
import ( import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
) )
// NotaryType is a notary event enumeration type. // NotaryType is a notary event enumeration type.
@ -39,3 +42,9 @@ func NotaryTypeFromBytes(data []byte) NotaryType {
func NotaryTypeFromString(str string) NotaryType { func NotaryTypeFromString(str string) NotaryType {
return NotaryType(str) return NotaryType(str)
} }
// UnexpectedOpcode returns error when notary parsers
// get unexpected opcode in contract call.
func UnexpectedOpcode(method string, op opcode.Opcode) error {
return fmt.Errorf("unexpected opcode in %s call: %s", method, op)
}