Merge pull request #624 from nspcc-dev/feature/bigint

compiler: emit integers correctly
This commit is contained in:
Roman Khimov 2020-01-28 18:04:32 +03:00 committed by GitHub
commit 0ce0ecb95e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 18 deletions

View file

@ -7,7 +7,7 @@ import (
"math/big"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/CityOfZion/neo-go/pkg/vm"
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
)
@ -47,7 +47,7 @@ func emitInt(w *io.BinWriter, i int64) {
}
bInt := big.NewInt(i)
val := util.ArrayReverse(bInt.Bytes())
val := vm.IntToBytes(bInt)
emitBytes(w, val)
}

View file

@ -0,0 +1,26 @@
package compiler_test
import (
"bytes"
"fmt"
"math/big"
"testing"
)
// Test for #605, #623.
// Codegen should emit integers in proper format.
func TestManyVariables(t *testing.T) {
// any number with MSB=1 is suitable
// 155 was in the contract where this bug was first found.
const count = 155
buf := bytes.NewBufferString("package main\n")
for i := 0; i < count; i++ {
buf.WriteString(fmt.Sprintf("var a%d = %d\n", i, i))
}
buf.WriteString("func Main() int {\nreturn 7\n}\n")
src := buf.String()
eval(t, src, big.NewInt(7))
}

View file

@ -9,9 +9,9 @@ import (
// wordSizeBytes is a size of a big.Word (uint) in bytes.`
const wordSizeBytes = bits.UintSize / 8
// bytesToInt converts data in little-endian format to
// BytesToInt converts data in little-endian format to
// an integer.
func bytesToInt(data []byte) *big.Int {
func BytesToInt(data []byte) *big.Int {
n := new(big.Int)
size := len(data)
if size == 0 {
@ -79,8 +79,8 @@ func getEffectiveSize(buf []byte, isNeg bool) int {
return size
}
// intToBytes converts integer to a slice in little-endian format.
func intToBytes(n *big.Int) []byte {
// IntToBytes converts integer to a slice in little-endian format.
func IntToBytes(n *big.Int) []byte {
sign := n.Sign()
if sign == 0 {
return []byte{0}

View file

@ -106,19 +106,19 @@ var testCases = []struct {
func TestIntToBytes(t *testing.T) {
for _, tc := range testCases {
buf := intToBytes(big.NewInt(tc.number))
buf := IntToBytes(big.NewInt(tc.number))
assert.Equal(t, tc.buf, buf, "error while converting %d", tc.number)
}
}
func TestBytesToInt(t *testing.T) {
for _, tc := range testCases {
num := bytesToInt(tc.buf)
num := BytesToInt(tc.buf)
assert.Equal(t, tc.number, num.Int64(), "error while converting %d", tc.number)
}
t.Run("empty array", func(t *testing.T) {
require.EqualValues(t, 0, bytesToInt([]byte{}).Int64())
require.EqualValues(t, 0, BytesToInt([]byte{}).Int64())
})
}
@ -131,7 +131,7 @@ func TestEquivalentRepresentations(t *testing.T) {
buf = append(buf, 0xFF, 0xFF, 0xFF)
}
num := bytesToInt(buf)
num := BytesToInt(buf)
assert.Equal(t, tc.number, num.Int64(), "error while converting %d", tc.number)
}
}
@ -170,16 +170,16 @@ func TestVeryBigInts(t *testing.T) {
num, ok := new(big.Int).SetString(tc.numStr, 10)
assert.True(t, ok)
result := bytesToInt(tc.buf)
result := BytesToInt(tc.buf)
assert.Equal(t, num, result, "error while converting %s from bytes", tc.numStr)
assert.Equal(t, tc.buf, intToBytes(result), "error while converting %s to bytes", tc.numStr)
assert.Equal(t, tc.buf, IntToBytes(result), "error while converting %s to bytes", tc.numStr)
}
for _, tc := range stdlibCases {
num, ok := new(big.Int).SetString(tc.numStr, 10)
assert.True(t, ok)
result := bytesToInt(util.ArrayReverse(tc.buf))
result := BytesToInt(util.ArrayReverse(tc.buf))
assert.Equal(t, num, result, "error while converting %s from bytes", tc.numStr)
}
}

View file

@ -196,7 +196,7 @@ func compareItems(t *testing.T, a, b StackItem) {
case *BigIntegerItem:
require.Equal(t, val, ac.value.Int64())
case *ByteArrayItem:
require.Equal(t, val, bytesToInt(ac.value).Int64())
require.Equal(t, val, BytesToInt(ac.value).Int64())
case *BoolItem:
if ac.value {
require.Equal(t, val, int64(1))

View file

@ -48,7 +48,7 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
w.WriteBool(t.value)
case *BigIntegerItem:
w.WriteBytes([]byte{byte(integerT)})
w.WriteVarBytes(intToBytes(t.value))
w.WriteVarBytes(IntToBytes(t.value))
case *InteropItem:
w.Err = errors.New("not supported")
case *ArrayItem, *StructItem:
@ -106,7 +106,7 @@ func DecodeBinaryStackItem(r *io.BinReader) StackItem {
return NewBoolItem(b)
case integerT:
data := r.ReadVarBytes()
num := bytesToInt(data)
num := BytesToInt(data)
return &BigIntegerItem{
value: num,
}

View file

@ -81,7 +81,7 @@ func (e *Element) BigInt() *big.Int {
return big.NewInt(0)
default:
b := t.Value().([]uint8)
return bytesToInt(b)
return BytesToInt(b)
}
}

View file

@ -142,7 +142,7 @@ func NewBigIntegerItem(value int) *BigIntegerItem {
// Bytes converts i to a slice of bytes.
func (i *BigIntegerItem) Bytes() []byte {
return intToBytes(i.value)
return IntToBytes(i.value)
}
// Value implements StackItem interface.