sherlock-audit / 2024-06-allora-judging

0 stars 0 forks source link

0x416 - Does not check if the key exists when iterate over the map #136

Closed sherlock-admin4 closed 4 months ago

sherlock-admin4 commented 4 months ago

0x416

Medium

Does not check if the key exists when iterate over the map

Summary

Does not check if the key exists when iterate over the map

Vulnerability Detail

In file synth_palette_weight.go

func (p SynthPalette) Clone() SynthPalette {
    inferenceByWorker := make(map[Worker]*emissionstypes.Inference, len(p.InferenceByWorker))
    for k, v := range p.InferenceByWorker {
        inferenceCopy := *v
        inferenceByWorker[k] = &inferenceCopy
    }
    forecastByWorker := make(map[Worker]*emissionstypes.Forecast, len(p.ForecastByWorker))
    for k, v := range p.ForecastByWorker {
        forecastCopy := *v
        forecastByWorker[k] = &forecastCopy
    }
    forecastImpliedInferenceByWorker := make(map[Worker]*emissionstypes.Inference, len(p.ForecastImpliedInferenceByWorker))
    for k, v := range p.ForecastImpliedInferenceByWorker {
        inferenceCopy := *v
        forecastImpliedInferenceByWorker[k] = &inferenceCopy
    }
    infererRegrets := make(map[Worker]*StatefulRegret, len(p.InfererRegrets))
    for k, v := range p.InfererRegrets {
        regretCopy := *v
        infererRegrets[k] = &regretCopy
    }
    forecasterRegrets := make(map[Worker]*StatefulRegret, len(p.ForecasterRegrets))
    for k, v := range p.ForecasterRegrets {
        regretCopy := *v
        forecasterRegrets[k] = &regretCopy
    }

and

    infererWeights := make(map[Worker]Weight)
    forecasterWeights := make(map[Worker]Weight)
    if p.InferersNewStatus != InferersAllNewExceptOne {
        // Calculate the weights from the normalized regrets
        for address, worker := range p.InfererRegrets {
            // If there is more than one not-new inferer, calculate the weight for the ones that are not new
            var infererWeight = alloraMath.ZeroDec()
            if !worker.noPriorRegret {
                infererWeight, err = CalcWeightFromNormalizedRegret(normalizedInfererRegrets[address], maxRegret, p.PNorm, p.CNorm)
                if err != nil {
                    return RegretInformedWeights{}, errorsmod.Wrapf(err, "Error calculating inferer weight")
                }
            }
@           infererWeights[address] = infererWeight
        }

the code does not check if the key exists, and try to read and set value directly, which leads to panic and incorrect data.

Impact

the code does not check if the key exists, and try to read and set value directly, which leads to panic and incorrect data.

Code Snippet

synth_palette_weight.go

https://github.com/sherlock-audit/2024-06-allora/blob/4e1bc73db32873476f8b0a88945815d3978d931c/allora-chain/x/emissions/keeper/inference_synthesis/common.go#L37

Tool used

Manual Review

Recommendation

check if the key exists, if not exists, either init the value or continue the loop.

sherlock-admin3 commented 4 months ago

1 comment(s) were left on this issue during the judging contest.

0xmystery commented:

Key should always exist