Removed defer/panic. #503

This commit is contained in:
obscuren
2015-03-27 16:09:57 +01:00
parent 00f8319faf
commit 8a22cd5e6c
4 changed files with 181 additions and 121 deletions

View File

@ -15,28 +15,32 @@ func NewMemory() *Memory {
}
func (m *Memory) Set(offset, size uint64, value []byte) {
// If the length of the store is 0 this is a complete failure
// memory size is set prior to calling this method so enough size
// should always be available.
if len(m.store) == 0 {
// length of store may never be less than offset + size.
// The store should be resized PRIOR to setting the memory
if size > uint64(len(m.store)) {
panic("INVALID memory: store empty")
}
value = common.RightPadBytes(value, int(size))
totSize := offset + size
lenSize := int64(len(m.store) - 1)
if totSize > lenSize {
// Calculate the diff between the sizes
diff := totSize - lenSize
if diff > 0 {
// Create a new empty slice and append it
newSlice := make([]byte, diff-1)
// Resize slice
m.store = append(m.store, newSlice...)
}
// It's possible the offset is greater than 0 and size equals 0. This is because
// the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP)
if size > 0 {
copy(m.store[offset:offset+size], common.RightPadBytes(value, int(size)))
}
copy(m.store[offset:offset+size], value)
/*
totSize := offset + size
lenSize := uint64(len(m.store) - 1)
if totSize > lenSize {
// Calculate the diff between the sizes
diff := totSize - lenSize
if diff > 0 {
// Create a new empty slice and append it
newSlice := make([]byte, diff-1)
// Resize slice
m.store = append(m.store, newSlice...)
}
}
*/
}
func (m *Memory) Resize(size uint64) {