Skip to content

Commit

Permalink
Mitigate potential overflow. ethereum/consensus-specs#2129 (#7795)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon committed Nov 12, 2020
1 parent 47daeda commit 5f92395
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions beacon-chain/core/helpers/block.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/core/helpers/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers_test

import (
"fmt"
"math"
"testing"

"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5f92395

Please sign in to comment.