beacon/light: fix potential database iterator leaking

This commit is contained in:
Gary Rong 2023-12-08 13:51:48 +08:00
parent 3b38b3b8ac
commit 751dc7a8a6

View file

@ -48,6 +48,8 @@ func newCanonicalStore[T any](db ethdb.Iteratee, keyPrefix []byte) (*canonicalSt
kl = len(keyPrefix) kl = len(keyPrefix)
first = true first = true
) )
defer iter.Release()
for iter.Next() { for iter.Next() {
if len(iter.Key()) != kl+8 { if len(iter.Key()) != kl+8 {
log.Warn("Invalid key length in the canonical chain database", "key", fmt.Sprintf("%#x", iter.Key())) log.Warn("Invalid key length in the canonical chain database", "key", fmt.Sprintf("%#x", iter.Key()))
@ -57,12 +59,11 @@ func newCanonicalStore[T any](db ethdb.Iteratee, keyPrefix []byte) (*canonicalSt
if first { if first {
cs.periods.Start = period cs.periods.Start = period
} else if cs.periods.End != period { } else if cs.periods.End != period {
return nil, fmt.Errorf("Gap in the canonical chain database between periods %d and %d", cs.periods.End, period-1) return nil, fmt.Errorf("gap in the canonical chain database between periods %d and %d", cs.periods.End, period-1)
} }
first = false first = false
cs.periods.End = period + 1 cs.periods.End = period + 1
} }
iter.Release()
return cs, nil return cs, nil
} }