diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-06-28 18:12:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-28 18:12:13 +0800 |
commit | dfd076244dd0c2d809f9dd0080feab167ba9560c (patch) | |
tree | 5791af8e261c51e0ebde666a0a6086921d27279c /core | |
parent | 6dc32e897a0e71982adbbf7123e19115f27f7135 (diff) | |
parent | e4301564c2af0b4f084a3bcf8ddcec5e84ac4ead (diff) | |
download | dexon-dfd076244dd0c2d809f9dd0080feab167ba9560c.tar dexon-dfd076244dd0c2d809f9dd0080feab167ba9560c.tar.gz dexon-dfd076244dd0c2d809f9dd0080feab167ba9560c.tar.bz2 dexon-dfd076244dd0c2d809f9dd0080feab167ba9560c.tar.lz dexon-dfd076244dd0c2d809f9dd0080feab167ba9560c.tar.xz dexon-dfd076244dd0c2d809f9dd0080feab167ba9560c.tar.zst dexon-dfd076244dd0c2d809f9dd0080feab167ba9560c.zip |
Merge pull request #14718 from holiman/gascalc_fix
core/vm: fix overflow in gas calculation formula
Diffstat (limited to 'core')
-rw-r--r-- | core/vm/gas_table.go | 16 | ||||
-rw-r--r-- | core/vm/gas_table_test.go | 18 |
2 files changed, 17 insertions, 17 deletions
diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 24ad6caa5..761ca4450 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -17,7 +17,6 @@ package vm import ( - gmath "math" "math/big" "github.com/ethereum/go-ethereum/common" @@ -28,15 +27,20 @@ import ( // memoryGasCosts 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) { - // The maximum that will fit in a uint64 is max_word_count - 1 - // anything above that will result in an overflow. - if newMemSize > gmath.MaxUint64-32 { - return 0, errGasUintOverflow - } 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 { + 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 1ee909e92..1b91aee56 100644 --- a/core/vm/gas_table_test.go +++ b/core/vm/gas_table_test.go @@ -16,24 +16,20 @@ package vm -import ( - "math" - "testing" -) +import "testing" func TestMemoryGasCost(t *testing.T) { - size := uint64(math.MaxUint64 - 64) - _, err := memoryGasCost(&Memory{}, size) + //size := uint64(math.MaxUint64 - 64) + size := uint64(0xffffffffe0) + v, err := memoryGasCost(&Memory{}, size) if err != nil { t.Error("didn't expect error:", err) } - - _, err = memoryGasCost(&Memory{}, size+32) - if err != nil { - t.Error("didn't expect error:", err) + if v != 36028899963961341 { + t.Errorf("Expected: 36028899963961341, got %d", v) } - _, err = memoryGasCost(&Memory{}, size+33) + _, err = memoryGasCost(&Memory{}, size+1) if err == nil { t.Error("expected error") } |