Skip to content

Commit 6be83e2

Browse files
committed
btf: don't access Spec.types from iterator
Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
1 parent 5266d91 commit 6be83e2

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

btf/btf.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,25 @@ func (s *Spec) nextTypeID() (TypeID, error) {
531531
// Returns an error wrapping ErrNotFound if a Type with the given ID
532532
// does not exist in the Spec.
533533
func (s *Spec) TypeByID(id TypeID) (Type, error) {
534-
if id < s.firstTypeID {
534+
typ, ok := s.typeByID(id)
535+
if !ok {
535536
return nil, fmt.Errorf("look up type with ID %d (first ID is %d): %w", id, s.firstTypeID, ErrNotFound)
536537
}
537538

539+
return typ, nil
540+
}
541+
542+
func (s *Spec) typeByID(id TypeID) (Type, bool) {
543+
if id < s.firstTypeID {
544+
return nil, false
545+
}
546+
538547
index := int(id - s.firstTypeID)
539548
if index >= len(s.types) {
540-
return nil, fmt.Errorf("look up type with ID %d: %w", id, ErrNotFound)
549+
return nil, false
541550
}
542551

543-
return s.types[index], nil
552+
return s.types[index], true
544553
}
545554

546555
// TypeID returns the ID for a given Type.
@@ -671,26 +680,27 @@ func LoadSplitSpecFromReader(r io.ReaderAt, base *Spec) (*Spec, error) {
671680

672681
// TypesIterator iterates over types of a given spec.
673682
type TypesIterator struct {
674-
types []Type
675-
index int
683+
spec *Spec
684+
id TypeID
685+
done bool
676686
// The last visited type in the spec.
677687
Type Type
678688
}
679689

680690
// Iterate returns the types iterator.
681691
func (s *Spec) Iterate() *TypesIterator {
682-
// We share the backing array of types with the Spec. This is safe since
683-
// we don't allow deletion or shuffling of types.
684-
return &TypesIterator{types: s.types, index: 0}
692+
return &TypesIterator{spec: s, id: s.firstTypeID}
685693
}
686694

687695
// Next returns true as long as there are any remaining types.
688696
func (iter *TypesIterator) Next() bool {
689-
if len(iter.types) <= iter.index {
697+
if iter.done {
690698
return false
691699
}
692700

693-
iter.Type = iter.types[iter.index]
694-
iter.index++
695-
return true
701+
var ok bool
702+
iter.Type, ok = iter.spec.typeByID(iter.id)
703+
iter.id++
704+
iter.done = !ok
705+
return !iter.done
696706
}

0 commit comments

Comments
 (0)