Skip to content

Commit

Permalink
Merge pull request ethereum#14718 from holiman/gascalc_fix
Browse files Browse the repository at this point in the history
core/vm: fix overflow in gas calculation formula
  • Loading branch information
karalabe authored Jun 28, 2017
2 parents 6dc32e8 + e430156 commit dfd0762
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
16 changes: 10 additions & 6 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package vm

import (
gmath "math"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -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
Expand Down
18 changes: 7 additions & 11 deletions core/vm/gas_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit dfd0762

Please sign in to comment.