Skip to content

Commit

Permalink
Compare with nil before invoking IsNil() on an interface (#14431)
Browse files Browse the repository at this point in the history
* Compare with nil before invoking `IsNil()` on an interface

* changelog

* review
  • Loading branch information
rkapka authored Sep 6, 2024
1 parent eec3b0b commit 62b8e63
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
- E2E: fixed gas limit at genesis
- Light client support: use LightClientHeader instead of BeaconBlockHeader.
- Core: Fix process effective balance update to safe copy validator for Electra.
- `== nil` checks before calling `IsNil()` on interfaces to prevent panics.

### Security

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *Service) notifyForkchoiceUpdate(ctx context.Context, arg *fcuConfig) (*
ctx, span := trace.StartSpan(ctx, "blockChain.notifyForkchoiceUpdate")
defer span.End()

if arg.headBlock.IsNil() {
if arg.headBlock == nil || arg.headBlock.IsNil() {
log.Error("Head block is nil")
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/pow_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *Service) validateMergeBlock(ctx context.Context, b interfaces.ReadOnlyS
if err != nil {
return err
}
if payload.IsNil() {
if payload == nil || payload.IsNil() {
return errors.New("nil execution payload")
}
ok, err := canUseValidatedTerminalBlockHash(b.Block().Slot(), payload)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/execution/engine_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (s *Service) ReconstructFullBellatrixBlockBatch(
func fullPayloadFromPayloadBody(
header interfaces.ExecutionData, body *pb.ExecutionPayloadBody, bVersion int,
) (interfaces.ExecutionData, error) {
if header.IsNil() || body == nil {
if header == nil || header.IsNil() || body == nil {
return nil, errors.New("execution block and header cannot be nil")
}

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/execution/payload_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *blindedBlockReconstructor) addToBatch(b interfaces.ReadOnlySignedBeacon
if err != nil {
return err
}
if header.IsNil() {
if header == nil || header.IsNil() {
return errors.New("execution payload header in blinded block was nil")
}
r.orderedBlocks = append(r.orderedBlocks, &blockWithHeader{block: b, header: header})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (vs *Server) getPayloadHeaderFromBuilder(ctx context.Context, slot primitiv
if err != nil {
return nil, err
}
if signedBid.IsNil() {
if signedBid == nil || signedBid.IsNil() {
return nil, errors.New("builder returned nil bid")
}
fork, err := forks.Fork(slots.ToEpoch(slot))
Expand All @@ -217,7 +217,7 @@ func (vs *Server) getPayloadHeaderFromBuilder(ctx context.Context, slot primitiv
if err != nil {
return nil, errors.Wrap(err, "could not get bid")
}
if bid.IsNil() {
if bid == nil || bid.IsNil() {
return nil, errors.New("builder returned nil bid")
}

Expand Down Expand Up @@ -309,14 +309,14 @@ func validateBuilderSignature(signedBid builder.SignedBid) error {
if err != nil {
return err
}
if signedBid.IsNil() {
if signedBid == nil || signedBid.IsNil() {
return errors.New("nil builder bid")
}
bid, err := signedBid.Message()
if err != nil {
return errors.Wrap(err, "could not get bid")
}
if bid.IsNil() {
if bid == nil || bid.IsNil() {
return errors.New("builder returned nil bid")
}
return signing.VerifySigningRoot(bid, bid.Pubkey(), signedBid.Signature(), d)
Expand Down
7 changes: 2 additions & 5 deletions beacon-chain/rpc/prysm/v1alpha1/validator/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,13 @@ func statusForPubKey(headState state.ReadOnlyBeaconState, pubKey []byte) (ethpb.

func assignmentStatus(beaconState state.ReadOnlyBeaconState, validatorIndex primitives.ValidatorIndex) ethpb.ValidatorStatus {
validator, err := beaconState.ValidatorAtIndexReadOnly(validatorIndex)
if err != nil {
if err != nil || validator.IsNil() {
return ethpb.ValidatorStatus_UNKNOWN_STATUS
}

currentEpoch := time.CurrentEpoch(beaconState)
farFutureEpoch := params.BeaconConfig().FarFutureEpoch
validatorBalance := validator.EffectiveBalance()

if validator.IsNil() {
return ethpb.ValidatorStatus_UNKNOWN_STATUS
}
if currentEpoch < validator.ActivationEligibilityEpoch() {
return depositStatus(validatorBalance)
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/sync/validate_beacon_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (s *Service) validateBellatrixBeaconBlock(ctx context.Context, parentState
if err != nil {
return err
}
if payload.IsNil() {
if payload == nil || payload.IsNil() {
return errors.New("execution payload is nil")
}
if payload.Timestamp() != uint64(t.Unix()) {
Expand Down
2 changes: 1 addition & 1 deletion consensus-types/blocks/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (b *SignedBeaconBlock) ToBlinded() (interfaces.ReadOnlySignedBeaconBlock, e
}

func (b *SignedBeaconBlock) Unblind(e interfaces.ExecutionData) error {
if e.IsNil() {
if e == nil || e.IsNil() {
return errors.New("cannot unblind with nil execution data")
}
if !b.IsBlinded() {
Expand Down
4 changes: 2 additions & 2 deletions testing/endtoend/evaluators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func validatorsSyncParticipation(_ *types.EvaluationContext, conns ...*grpc.Clie
return errors.Wrapf(err, "block type doesn't exist for block at epoch %d", lowestBound)
}

if b.IsNil() {
if b == nil || b.IsNil() {
return errors.New("nil block provided")
}
forkStartSlot, err := slots.EpochStart(params.BeaconConfig().AltairForkEpoch)
Expand Down Expand Up @@ -274,7 +274,7 @@ func validatorsSyncParticipation(_ *types.EvaluationContext, conns ...*grpc.Clie
return errors.Wrapf(err, "block type doesn't exist for block at epoch %d", lowestBound)
}

if b.IsNil() {
if b == nil || b.IsNil() {
return errors.New("nil block provided")
}
forkSlot, err := slots.EpochStart(params.BeaconConfig().AltairForkEpoch)
Expand Down

0 comments on commit 62b8e63

Please sign in to comment.