Right pad bytes to prevent future programmers making bugs

This commit is contained in:
obscuren 2015-03-20 20:37:56 +01:00
parent f4e9638867
commit abce6804a0
1 changed files with 18 additions and 14 deletions

View File

@ -1,6 +1,10 @@
package vm package vm
import "fmt" import (
"fmt"
"github.com/ethereum/go-ethereum/common"
)
type Memory struct { type Memory struct {
store []byte store []byte
@ -11,21 +15,21 @@ func NewMemory() *Memory {
} }
func (m *Memory) Set(offset, size uint64, value []byte) { func (m *Memory) Set(offset, size uint64, value []byte) {
if len(value) > 0 { value = common.RightPadBytes(value, int(size))
totSize := offset + size
lenSize := uint64(len(m.store) - 1) totSize := offset + size
if totSize > lenSize { lenSize := uint64(len(m.store) - 1)
// Calculate the diff between the sizes if totSize > lenSize {
diff := totSize - lenSize // Calculate the diff between the sizes
if diff > 0 { diff := totSize - lenSize
// Create a new empty slice and append it if diff > 0 {
newSlice := make([]byte, diff-1) // Create a new empty slice and append it
// Resize slice newSlice := make([]byte, diff-1)
m.store = append(m.store, newSlice...) // Resize slice
} m.store = append(m.store, newSlice...)
} }
copy(m.store[offset:offset+size], value)
} }
copy(m.store[offset:offset+size], value)
} }
func (m *Memory) Resize(size uint64) { func (m *Memory) Resize(size uint64) {