Skip to content

Commit abce680

Browse files
committed
Right pad bytes to prevent future programmers making bugs
1 parent f4e9638 commit abce680

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

‎vm/memory.go‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package vm
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
)
48

59
type Memory struct {
610
store []byte
@@ -11,21 +15,21 @@ func NewMemory() *Memory {
1115
}
1216

1317
func (m *Memory) Set(offset, size uint64, value []byte) {
14-
if len(value) > 0 {
15-
totSize := offset + size
16-
lenSize := uint64(len(m.store) - 1)
17-
if totSize > lenSize {
18-
// Calculate the diff between the sizes
19-
diff := totSize - lenSize
20-
if diff > 0 {
21-
// Create a new empty slice and append it
22-
newSlice := make([]byte, diff-1)
23-
// Resize slice
24-
m.store = append(m.store, newSlice...)
25-
}
18+
value = common.RightPadBytes(value, int(size))
19+
20+
totSize := offset + size
21+
lenSize := uint64(len(m.store) - 1)
22+
if totSize > lenSize {
23+
// Calculate the diff between the sizes
24+
diff := totSize - lenSize
25+
if diff > 0 {
26+
// Create a new empty slice and append it
27+
newSlice := make([]byte, diff-1)
28+
// Resize slice
29+
m.store = append(m.store, newSlice...)
2630
}
27-
copy(m.store[offset:offset+size], value)
2831
}
32+
copy(m.store[offset:offset+size], value)
2933
}
3034

3135
func (m *Memory) Resize(size uint64) {

0 commit comments

Comments
 (0)