Skip to content

Commit

Permalink
fix opcode xtuck panic (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy committed Jun 3, 2019
1 parent 0fc1266 commit cac00a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion vm/neovm/func_flowcontrol.go
Expand Up @@ -78,7 +78,7 @@ func opDCALL(e *ExecutionEngine) (VMState, error) {
return FAULT, errors.ERR_DCALL_OFFSET_ERROR
}

if dest.Sign() < 0 || dest.Cmp(big.NewInt(int64(len(e.Context.Code)))) > 0 {
if dest.Sign() < 0 || dest.Cmp(big.NewInt(int64(len(e.Context.Code)))) >= 0 {
return FAULT, errors.ERR_DCALL_OFFSET_ERROR
}

Expand Down
6 changes: 3 additions & 3 deletions vm/neovm/func_stack_test.go
Expand Up @@ -141,10 +141,10 @@ func TestOpXTuck(t *testing.T) {
stack.Push(types.NewInteger(big.NewInt(8888)))
stack.Push(types.NewInteger(big.NewInt(7777)))

stack.Push(NewStackItem(types.NewInteger(big.NewInt(2))))
stack.Push(NewStackItem(types.NewInteger(big.NewInt(0))))
e.EvaluationStack = stack

opXSwap(&e)
opXTuck(&e)
v1, err := stack.Peek(0).GetBigInteger()
if err != nil {
t.Fatal("NeoVM OpXTuck test failed.")
Expand All @@ -156,7 +156,7 @@ func TestOpXTuck(t *testing.T) {
e1 := v1.Int64()
e2 := v2.Int64()

if stack.Count() != 3 || e1 != 9999 || e2 != 7777 {
if stack.Count() != 4 || e1 != 7777 || e2 != 8888 {
t.Fatal("NeoVM OpXTuck test failed.")
}
}
Expand Down
15 changes: 14 additions & 1 deletion vm/neovm/func_validate.go
Expand Up @@ -157,7 +157,20 @@ func validateXSwap(e *ExecutionEngine) error {
}

func validateXTuck(e *ExecutionEngine) error {
return validateOpStack(e, "[validateXTuck]")
total := EvaluationStackCount(e)
if total < 1 {
return errors.ERR_UNDER_STACK_LEN
}
index, err := PeekBigInteger(e)
if err != nil {
return err
}
count := big.NewInt(0)
if index.Sign() < 0 || count.Add(index, big.NewInt(1)).Cmp(big.NewInt(int64(total))) > 0 {
return errors.ERR_BAD_VALUE
}

return nil
}

func validatePick(e *ExecutionEngine) error {
Expand Down
4 changes: 2 additions & 2 deletions vm/neovm/stack.go
Expand Up @@ -47,8 +47,8 @@ func (r *RandomAccessStack) Insert(index int, t types.StackItems) {
return
}
index = l - index
r.e = append(r.e, r.e[l-1])
copy(r.e[index+1:l], r.e[index:])
r.e = append(r.e, t)
copy(r.e[index+1:], r.e[index:])
r.e[index] = t
}

Expand Down

0 comments on commit cac00a2

Please sign in to comment.