From bb8de5a7967f7d3369ee71ef311605947bfe4475 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 16 Oct 2024 19:59:14 -0700 Subject: [PATCH] Make a copy of the multiaddr slice in Addrs() --- p2p/host/basic/basic_host.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 1ec51021f2..4f076d38b4 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -843,8 +843,12 @@ 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) // Add certhashes for the addresses provided by the user via address factory. - return h.addCertHashes(ma.Unique(h.AddrsFactory(h.AllAddrs()))) + return h.addCertHashes(ma.Unique(res)) } // NormalizeMultiaddr returns a multiaddr suitable for equality checks.