Mitigate potential overflow. ethereum/eth2.0-specs#2129 (#7795)

This commit is contained in:
Preston Van Loon 2020-11-12 12:28:19 -08:00 committed by GitHub
parent 47daedaf11
commit 5f9239595b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package helpers
import (
"math"
"github.com/pkg/errors"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/params"
@ -17,6 +19,9 @@ import (
// assert slot < state.slot <= slot + SLOTS_PER_HISTORICAL_ROOT
// return state.block_roots[slot % SLOTS_PER_HISTORICAL_ROOT]
func BlockRootAtSlot(state *stateTrie.BeaconState, slot uint64) ([]byte, error) {
if math.MaxUint64-slot < params.BeaconConfig().SlotsPerHistoricalRoot {
return []byte{}, errors.New("slot overflows uint64")
}
if slot >= state.Slot() || state.Slot() > slot+params.BeaconConfig().SlotsPerHistoricalRoot {
return []byte{}, errors.Errorf("slot %d out of bounds", slot)
}

View File

@ -2,6 +2,7 @@ package helpers_test
import (
"fmt"
"math"
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
@ -101,6 +102,11 @@ func TestBlockRootAtSlot_OutOfBounds(t *testing.T) {
stateSlot: params.BeaconConfig().SlotsPerHistoricalRoot + 2,
expectedErr: "slot 1 out of bounds",
},
{
slot: math.MaxUint64 - 5,
stateSlot: 0, // Doesn't matter
expectedErr: "slot overflows uint64",
},
}
for _, tt := range tests {
state.Slot = tt.stateSlot