File tree Expand file tree Collapse file tree 2 files changed +17
-17
lines changed Expand file tree Collapse file tree 2 files changed +17
-17
lines changed Original file line number Diff line number Diff line change 17
17
package vm
18
18
19
19
import (
20
- gmath "math"
21
20
"math/big"
22
21
23
22
"github.com/ethereum/go-ethereum/common"
@@ -28,15 +27,20 @@ import (
28
27
// memoryGasCosts calculates the quadratic gas for memory expansion. It does so
29
28
// only for the memory region that is expanded, not the total memory.
30
29
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
- }
36
30
37
31
if newMemSize == 0 {
38
32
return 0 , nil
39
33
}
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
+ }
40
44
41
45
newMemSizeWords := toWordSize (newMemSize )
42
46
newMemSize = newMemSizeWords * 32
Original file line number Diff line number Diff line change 16
16
17
17
package vm
18
18
19
- import (
20
- "math"
21
- "testing"
22
- )
19
+ import "testing"
23
20
24
21
func TestMemoryGasCost (t * testing.T ) {
25
- size := uint64 (math .MaxUint64 - 64 )
26
- _ , err := memoryGasCost (& Memory {}, size )
22
+ //size := uint64(math.MaxUint64 - 64)
23
+ size := uint64 (0xffffffffe0 )
24
+ v , err := memoryGasCost (& Memory {}, size )
27
25
if err != nil {
28
26
t .Error ("didn't expect error:" , err )
29
27
}
30
-
31
- _ , err = memoryGasCost (& Memory {}, size + 32 )
32
- if err != nil {
33
- t .Error ("didn't expect error:" , err )
28
+ if v != 36028899963961341 {
29
+ t .Errorf ("Expected: 36028899963961341, got %d" , v )
34
30
}
35
31
36
- _ , err = memoryGasCost (& Memory {}, size + 33 )
32
+ _ , err = memoryGasCost (& Memory {}, size + 1 )
37
33
if err == nil {
38
34
t .Error ("expected error" )
39
35
}
You can’t perform that action at this time.
0 commit comments