aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2019-04-01 22:12:13 +0800
committerGitHub <noreply@github.com>2019-04-01 22:12:13 +0800
commit92faf1bf7a0aee0bd42187420829625c7fcbde3f (patch)
tree767991b6354e6aeee17baa589e7e8fbe4bdb6706
parentcd79bc61a983d6482579d12cdd239b37bbfa12ef (diff)
parent9294b8f10f1fceb662a06f9d45aaf7a6b2492f05 (diff)
downloadgo-tangerine-92faf1bf7a0aee0bd42187420829625c7fcbde3f.tar
go-tangerine-92faf1bf7a0aee0bd42187420829625c7fcbde3f.tar.gz
go-tangerine-92faf1bf7a0aee0bd42187420829625c7fcbde3f.tar.bz2
go-tangerine-92faf1bf7a0aee0bd42187420829625c7fcbde3f.tar.lz
go-tangerine-92faf1bf7a0aee0bd42187420829625c7fcbde3f.tar.xz
go-tangerine-92faf1bf7a0aee0bd42187420829625c7fcbde3f.tar.zst
go-tangerine-92faf1bf7a0aee0bd42187420829625c7fcbde3f.zip
Merge pull request #19348 from LiangMa/overflowPR
core/vm: Correct the Memory Gas Overflow condition
-rw-r--r--core/vm/gas_table.go16
-rw-r--r--core/vm/gas_table_test.go27
2 files changed, 21 insertions, 22 deletions
diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go
index 6400c1324..8270300ba 100644
--- a/core/vm/gas_table.go
+++ b/core/vm/gas_table.go
@@ -25,21 +25,17 @@ import (
// memoryGasCost calculates the quadratic gas for memory expansion. It does so
// only for the memory region that is expanded, not the total memory.
func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) {
-
if newMemSize == 0 {
return 0, nil
}
- // 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 0x7ffffffff will cause the square operation
- // to overflow.
- // The constant 0xffffffffe0 is the highest number that can be used without
- // overflowing the gas calculation
- if newMemSize > 0xffffffffe0 {
+ // 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
+ // without overflowing the gas calculation.
+ if newMemSize > 0x1FFFFFFFE0 {
return 0, errGasUintOverflow
}
-
newMemSizeWords := toWordSize(newMemSize)
newMemSize = newMemSizeWords * 32
diff --git a/core/vm/gas_table_test.go b/core/vm/gas_table_test.go
index 1b91aee56..2c1e11894 100644
--- a/core/vm/gas_table_test.go
+++ b/core/vm/gas_table_test.go
@@ -19,18 +19,21 @@ package vm
import "testing"
func TestMemoryGasCost(t *testing.T) {
- //size := uint64(math.MaxUint64 - 64)
- size := uint64(0xffffffffe0)
- v, err := memoryGasCost(&Memory{}, size)
- if err != nil {
- t.Error("didn't expect error:", err)
+ tests := []struct {
+ size uint64
+ cost uint64
+ overflow bool
+ }{
+ {0x1fffffffe0, 36028809887088637, false},
+ {0x1fffffffe1, 0, true},
}
- if v != 36028899963961341 {
- t.Errorf("Expected: 36028899963961341, got %d", v)
- }
-
- _, err = memoryGasCost(&Memory{}, size+1)
- if err == nil {
- t.Error("expected error")
+ for i, tt := range tests {
+ v, err := memoryGasCost(&Memory{}, tt.size)
+ if (err == errGasUintOverflow) != tt.overflow {
+ t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == errGasUintOverflow, tt.overflow)
+ }
+ if v != tt.cost {
+ t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
+ }
}
}