Fix out of bound check in AncestorRoot (#7226)

* Move out of bound check to the correct scope
* Merge refs/heads/master into store-out-of-bound
This commit is contained in:
terence tsao 2020-09-14 02:10:52 -07:00 committed by GitHub
parent fcfd828725
commit e1aa920fc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -414,3 +414,24 @@ func TestStore_AncestorRoot(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, bytesutil.ToBytes32(r), [32]byte{'b'})
}
func TestStore_AncestorRootOutOfBound(t *testing.T) {
ctx := context.Background()
f := &ForkChoice{store: &Store{}}
f.store.nodesIndices = map[[32]byte]uint64{}
_, err := f.AncestorRoot(ctx, [32]byte{'a'}, 0)
assert.ErrorContains(t, "node does not exist", err)
f.store.nodesIndices[[32]byte{'a'}] = 0
_, err = f.AncestorRoot(ctx, [32]byte{'a'}, 0)
assert.ErrorContains(t, "node index out of range", err)
f.store.nodesIndices[[32]byte{'b'}] = 1
f.store.nodesIndices[[32]byte{'c'}] = 2
f.store.nodes = []*Node{
{slot: 1, root: [32]byte{'a'}, parent: NonExistentNode},
{slot: 2, root: [32]byte{'b'}, parent: 100}, // Out of bound parent.
{slot: 3, root: [32]byte{'c'}, parent: 1},
}
_, err = f.AncestorRoot(ctx, [32]byte{'c'}, 1)
require.ErrorContains(t, "node index out of range", err)
}

View File

@ -169,9 +169,10 @@ func (f *ForkChoice) AncestorRoot(ctx context.Context, root [32]byte, slot uint6
}
i = f.store.nodes[i].parent
}
if i >= uint64(len(f.store.nodes)) {
return nil, errors.New("node index out of range")
if i >= uint64(len(f.store.nodes)) {
return nil, errors.New("node index out of range")
}
}
return f.store.nodes[i].root[:], nil