From 439be0ce32c96db1a5aa47fad9f2719bd2fa8ff6 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:52:35 +0200 Subject: [PATCH] safe gas add (#98) * safe gas add * fix build --- core/vm/instructions.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index b73114bedffb..559c4e7918d5 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -18,6 +18,7 @@ package vm import ( "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" @@ -410,8 +411,12 @@ func touchEachChunksOnReadAndChargeGas(offset, size uint64, address []byte, code for i := offset / 31; i <= (endOffset-1)/31; i++ { index := trieUtils.GetTreeKeyCodeChunk(address, uint256.NewInt(i)) - // TODO safe-add here to catch overflow - statelessGasCharged += accesses.TouchAddressOnReadAndComputeGas(index) + var overflow bool + statelessGasCharged, overflow = math.SafeAdd(statelessGasCharged, accesses.TouchAddressOnReadAndComputeGas(index)) + if overflow { + panic("overflow when adding gas") + } + if code != nil && len(code) > 0 { if deployment { accesses.SetLeafValue(index[:], nil)