Skip to content

Commit

Permalink
btf: fix data race in (*mutableTypes).copy
Browse files Browse the repository at this point in the history
Move the creation of the mutableTypes copy under the read lock, so that
the access to the len of the source types is protected when
pre-allocating the maps.

Signed-off-by: Paul Cacheux <[email protected]>
  • Loading branch information
paulcacheux authored and lmb committed Feb 10, 2025
1 parent 1bcc12e commit 0cd3acc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ func (mt *mutableTypes) copy() *mutableTypes {
return nil
}

// Prevent concurrent modification of mt.copiedTypeIDs.
mt.mu.RLock()
defer mt.mu.RUnlock()

mtCopy := &mutableTypes{
mt.imm,
sync.RWMutex{},
make(map[Type]Type, len(mt.copies)),
make(map[Type]TypeID, len(mt.copiedTypeIDs)),
}

// Prevent concurrent modification of mt.copiedTypeIDs.
mt.mu.RLock()
defer mt.mu.RUnlock()

copiesOfCopies := make(map[Type]Type, len(mt.copies))
for orig, copy := range mt.copies {
// NB: We make a copy of copy, not orig, so that changes to mutable types
Expand Down

0 comments on commit 0cd3acc

Please sign in to comment.