mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-31 17:13:57 +00:00
47 lines
1.6 KiB
Go
47 lines
1.6 KiB
Go
// Copyright 2023 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library 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 go-ethereum library 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
package directory
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
)
|
|
|
|
const (
|
|
memoryPadLimit = 1024 * 1024
|
|
)
|
|
|
|
// GetMemoryCopyPadded returns offset + size as a new slice.
|
|
// It zero-pads the slice if it extends beyond memory bounds.
|
|
func GetMemoryCopyPadded(m *vm.Memory, offset, size int64) ([]byte, error) {
|
|
if offset < 0 || size < 0 {
|
|
return nil, errors.New("offset or size must not be negative")
|
|
}
|
|
if int(offset+size) < m.Len() { // slice fully inside memory
|
|
return m.GetCopy(offset, size), nil
|
|
}
|
|
paddingNeeded := int(offset+size) - m.Len()
|
|
if paddingNeeded > memoryPadLimit {
|
|
return nil, fmt.Errorf("reached limit for padding memory slice: %d", paddingNeeded)
|
|
}
|
|
cpy := make([]byte, size)
|
|
if overlap := int64(m.Len()) - offset; overlap > 0 {
|
|
copy(cpy, m.GetPtr(offset, overlap))
|
|
}
|
|
return cpy, nil
|
|
}
|