Skip to content

Commit 8423de3

Browse files
authored
basichost: avoid modifying slice returned by AddrsFactory (#3068)
1 parent f421202 commit 8423de3

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error {
582582
if cfg.AddrsFactory != nil {
583583
addrFunc = func() []ma.Multiaddr {
584584
return slices.DeleteFunc(
585-
cfg.AddrsFactory(h.AllAddrs()),
585+
slices.Clone(cfg.AddrsFactory(h.AllAddrs())),
586586
func(a ma.Multiaddr) bool { return !manet.IsPublicAddr(a) })
587587
}
588588
}

p2p/host/basic/basic_host.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
271271
h.hps, err = holepunch.NewService(h, h.ids, func() []ma.Multiaddr {
272272
addrs := h.AllAddrs()
273273
if opts.AddrsFactory != nil {
274-
addrs = opts.AddrsFactory(addrs)
274+
addrs = slices.Clone(opts.AddrsFactory(addrs))
275275
}
276276
// AllAddrs may ignore observed addresses in favour of NAT mappings. Use both for hole punching.
277277
addrs = append(addrs, h.ids.OwnObservedAddrs()...)
@@ -841,12 +841,10 @@ func (h *BasicHost) ConnManager() connmgr.ConnManager {
841841
// When used with AutoRelay, and if the host is not publicly reachable,
842842
// this will only have host's private, relay, and no public addresses.
843843
func (h *BasicHost) Addrs() []ma.Multiaddr {
844-
addrs := h.AddrsFactory(h.AllAddrs())
845844
// Make a copy. Consumers can modify the slice elements
846-
res := make([]ma.Multiaddr, len(addrs))
847-
copy(res, addrs)
845+
addrs := slices.Clone(h.AddrsFactory(h.AllAddrs()))
848846
// Add certhashes for the addresses provided by the user via address factory.
849-
return h.addCertHashes(ma.Unique(res))
847+
return h.addCertHashes(ma.Unique(addrs))
850848
}
851849

852850
// NormalizeMultiaddr returns a multiaddr suitable for equality checks.

0 commit comments

Comments
 (0)