attestantio / go-eth2-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Slice of pointers can json unmarshal with nil values

jtraglia opened this issue · comments

Hi there. When unmarshaling structures with a slice of pointers, we do not check that the values are not nil. Therefore, it's possible for these values to be nil after unmarshaling and no error is reported. Depending on how the result is used, this could result in a nil dereference error. This is a pretty common pattern here, but for example, this structure:

ProposerSlashings []*phase0.ProposerSlashing `json:"proposer_slashings"`
AttesterSlashings []*phase0.AttesterSlashing `json:"attester_slashings"`
Attestations []*phase0.Attestation `json:"attestations"`
Deposits []*phase0.Deposit `json:"deposits"`
VoluntaryExits []*phase0.SignedVoluntaryExit `json:"voluntary_exits"`

When unmarshaling, we only check if the slice is nil, not the values:

if data.ProposerSlashings == nil {
return errors.New("proposer slashings missing")
}
b.ProposerSlashings = data.ProposerSlashings

One potential solution would to make these non-pointer types. Why are these pointers? The other solution would be to add the necessary nil checks when unmarshaling, which could be a lot of work.

Addressed in 595a7a6