Assessment reports>Initia>Low findings>Error not checked when fetching starting info
Category: Coding Mistakes

Error not checked when fetching starting info

Low Severity
Low Impact
Low Likelihood

Description

In CalculateDelegationRewards, the starting info for a delegation is fetching using the validator and delegator address pair:

valAddr, err := k.stakingKeeper.ValidatorAddressCodec().StringToBytes(del.GetValidatorAddr())
if err != nil {
    return nil, err
}

// fetch starting info for delegation
startingInfo, err := k.DelegatorStartingInfos.Get(ctx, collections.Join(valAddr, delAddr))

sdkCtx := sdk.UnwrapSDKContext(ctx)
if startingInfo.Height == uint64(sdkCtx.BlockHeight()) {
    // started this height, no rewards yet
    return
}

startingPeriod := startingInfo.PreviousPeriod
stakes := startingInfo.Stakes

The issue is that returned err is not checked, resulting in startingInfo being nil or containing unexpected values.

Impact

The DelegatorStartingInfos should always be set if there is a valid delegation, but if an error does occur, then it could cause a nil dereference panic or cause the starting info to contain unexpected values.

Recommendations

Any error returned from fetching a value from the store should either be handled correctly or explicitly ignored if that is the intended behavior.

Remediation

Zellic © 2024Back to top ↑