Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid modifying slice returned by AddrsFactory #3068

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error {
if cfg.AddrsFactory != nil {
addrFunc = func() []ma.Multiaddr {
return slices.DeleteFunc(
cfg.AddrsFactory(h.AllAddrs()),
slices.Clone(cfg.AddrsFactory(h.AllAddrs())),
func(a ma.Multiaddr) bool { return !manet.IsPublicAddr(a) })
}
}
Expand Down
8 changes: 3 additions & 5 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
h.hps, err = holepunch.NewService(h, h.ids, func() []ma.Multiaddr {
addrs := h.AllAddrs()
if opts.AddrsFactory != nil {
addrs = opts.AddrsFactory(addrs)
addrs = slices.Clone(opts.AddrsFactory(addrs))
}
// AllAddrs may ignore observed addresses in favour of NAT mappings. Use both for hole punching.
addrs = append(addrs, h.ids.OwnObservedAddrs()...)
Expand Down Expand Up @@ -841,12 +841,10 @@ func (h *BasicHost) ConnManager() connmgr.ConnManager {
// When used with AutoRelay, and if the host is not publicly reachable,
// this will only have host's private, relay, and no public addresses.
func (h *BasicHost) Addrs() []ma.Multiaddr {
addrs := h.AddrsFactory(h.AllAddrs())
// Make a copy. Consumers can modify the slice elements
res := make([]ma.Multiaddr, len(addrs))
copy(res, addrs)
addrs := slices.Clone(h.AddrsFactory(h.AllAddrs()))
// Add certhashes for the addresses provided by the user via address factory.
return h.addCertHashes(ma.Unique(res))
return h.addCertHashes(ma.Unique(addrs))
}

// NormalizeMultiaddr returns a multiaddr suitable for equality checks.
Expand Down
Loading