diff --git a/core/vm/common.go b/core/vm/common.go index 2990f58972..7688e772b5 100644 --- a/core/vm/common.go +++ b/core/vm/common.go @@ -23,6 +23,9 @@ import ( "github.com/holiman/uint256" ) +// MaxMemorySize is the largest size that won't overflow memory gas calculation. +const MaxMemorySize = 0x1FFFFFFFE0 + // calcMemSize64 calculates the required memory size, and returns // the size and whether the result overflowed uint64 func calcMemSize64(off, l *uint256.Int) (uint64, bool) { @@ -76,6 +79,11 @@ func getDataAndAdjustedBounds(data []byte, start uint64, size uint64) (codeCopyP return common.RightPadBytes(data[start:end], int(size)), start, end - start } +// ToWordSize returns the ceiled word size required for memory expansion. +func ToWordSize(size uint64) uint64 { + return toWordSize(size) +} + // toWordSize returns the ceiled word size required for memory expansion. func toWordSize(size uint64) uint64 { if size > math.MaxUint64-31 { diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index d482e4ee19..4f914bed33 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -35,9 +35,9 @@ func memoryGasCost(mem *Memory, newMemSize uint64) (multigas.MultiGas, error) { // The maximum that will fit in a uint64 is max_word_count - 1. Anything above // that will result in an overflow. Additionally, a newMemSize which results in // a newMemSizeWords larger than 0xFFFFFFFF will cause the square operation to - // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used + // overflow. The constant MaxMemorySize is the highest number that can be used // without overflowing the gas calculation. - if newMemSize > 0x1FFFFFFFE0 { + if newMemSize > MaxMemorySize { return multigas.ZeroGas(), ErrGasUintOverflow } newMemSizeWords := toWordSize(newMemSize)