Skip to content

Commit

Permalink
add array/struct append len check (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
carltraveler authored and laizy committed Jun 19, 2019
1 parent 459adb5 commit a9be329
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
54 changes: 54 additions & 0 deletions smartcontract/test/append_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package test

import (
"github.com/ontio/ontology/smartcontract"
"github.com/ontio/ontology/vm/neovm"
"github.com/ontio/ontology/vm/neovm/errors"
"github.com/stretchr/testify/assert"
"testing"
)

func TestAppendOverFlow(t *testing.T) {
// define 1024 len array
byteCode := []byte{
byte(0x04), //neovm.PUSHBYTES4
byte(0x00),
byte(0x04),
byte(0x00),
byte(0x00),
byte(neovm.NEWARRAY),
byte(neovm.PUSH2),
byte(neovm.APPEND),
}

config := &smartcontract.Config{
Time: 10,
Height: 10,
}
sc := smartcontract.SmartContract{
Config: config,
Gas: 200,
CacheDB: nil,
}
engine, _ := sc.NewExecuteEngine(byteCode)
_, err := engine.Invoke()
assert.EqualError(t, err, "[NeoVmService] vm execution error!: "+errors.ERR_OVER_MAX_ARRAY_SIZE.Error())
}
8 changes: 8 additions & 0 deletions vm/neovm/func_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ func validateNewArray(e *ExecutionEngine) error {
}

count, err := PeekBigInteger(e)

if err != nil {
return err
}
Expand Down Expand Up @@ -591,6 +592,13 @@ func validateAppend(e *ExecutionEngine) error {
if !ok1 && !ok2 {
return fmt.Errorf("validateAppend error: %s", errors.ERR_NOT_SUPPORT_TYPE)
}

arr, _ := arrItem.GetArray()
count := big.NewInt(int64(len(arr) + 1))
if count.Cmp(big.NewInt(int64(MAX_ARRAY_SIZE))) > 0 {
return errors.ERR_OVER_MAX_ARRAY_SIZE
}

return nil
}

Expand Down

0 comments on commit a9be329

Please sign in to comment.