Skip to content

Commit

Permalink
ensure no overflow occurs in the chunk touch function
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Mar 25, 2022
1 parent c9ffa89 commit dd6425e
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,8 @@ func touchEachChunksOnReadAndChargeGas(offset, size uint64, address []byte, code
}
var (
statelessGasCharged uint64
endLeafOffset uint64
startOffset uint64
endOffset uint64
numLeaves uint64
)
// startLeafOffset, endLeafOffset is the evm code offset of the first byte in the first leaf touched
// and the evm code offset of the last byte in the last leaf touched
Expand All @@ -405,10 +403,8 @@ func touchEachChunksOnReadAndChargeGas(offset, size uint64, address []byte, code
} else {
endOffset = startOffset + size
}
endLeafOffset = endOffset + (endOffset % 31)
numLeaves = (endLeafOffset - startOffset + 30) / 31

for i := 0; i < int(numLeaves); i++ {
for i := offset / 31; i < endOffset/31; i++ {
index := trieUtils.GetTreeKeyCodeChunk(address, uint256.NewInt(uint64(i)+startOffset/31))

// TODO safe-add here to catch overflow
Expand All @@ -424,7 +420,10 @@ func touchEachChunksOnReadAndChargeGas(offset, size uint64, address []byte, code
if curEnd > endOffset {
curEnd = endOffset
}
valueSize := curEnd - (uint64(i) * 31)
var valueSize uint64
if curEnd >= (uint64(i) * 31) {
valueSize = curEnd - (uint64(i) * 31)
}
value = make([]byte, 32, 32)
value[0] = byte(firstPushOffset)

Expand Down

0 comments on commit dd6425e

Please sign in to comment.