Skip to content

Commit bae7565

Browse files
committed
core/vm: fix overflow in gas calculation formula
1 parent 9e5f03b commit bae7565

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

core/vm/gas_table.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package vm
1818

1919
import (
20-
gmath "math"
2120
"math/big"
2221

2322
"github.com/ethereum/go-ethereum/common"
@@ -28,15 +27,20 @@ import (
2827
// memoryGasCosts calculates the quadratic gas for memory expansion. It does so
2928
// only for the memory region that is expanded, not the total memory.
3029
func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) {
31-
// The maximum that will fit in a uint64 is max_word_count - 1
32-
// anything above that will result in an overflow.
33-
if newMemSize > gmath.MaxUint64-32 {
34-
return 0, errGasUintOverflow
35-
}
3630

3731
if newMemSize == 0 {
3832
return 0, nil
3933
}
34+
// The maximum that will fit in a uint64 is max_word_count - 1
35+
// anything above that will result in an overflow.
36+
// Additionally, a newMemSize which results in a
37+
// newMemSizeWords larger than 0x7ffffffff will cause the square operation
38+
// to overflow.
39+
// The constant 0xffffffffe0 is the highest number that can be used without
40+
// overflowing the gas calculation
41+
if newMemSize > 0xffffffffe0 {
42+
return 0, errGasUintOverflow
43+
}
4044

4145
newMemSizeWords := toWordSize(newMemSize)
4246
newMemSize = newMemSizeWords * 32

0 commit comments

Comments
 (0)