Passing const uint8_t*
will not work as long as the argument of the method is a reference to an array: CNetAddr::SetLegacyIPv6(const uint8_t (&ipv6)[ADDR_IPV6_SIZE])
which is best of type safety but unfortunately two of the callers don’t have an array to pass.
I think either one of the following two is better:
0diff --git i/src/netaddress.cpp w/src/netaddress.cpp
1index b421e1b11..bb4d2b50b 100644
2--- i/src/netaddress.cpp
3+++ w/src/netaddress.cpp
4@@ -26,32 +26,33 @@ CNetAddr::CNetAddr() {}
5 void CNetAddr::SetIP(const CNetAddr& ipIn)
6 {
7 m_net = ipIn.m_net;
8 m_addr = ipIn.m_addr;
9 }
10
11-void CNetAddr::SetLegacyIPv6(const uint8_t (&ipv6)[ADDR_IPV6_SIZE])
12+void CNetAddr::SetLegacyIPv6(const void* ipv6)
13 {
14- const uint8_t* ipv6_end = ipv6 + ADDR_IPV6_SIZE;
15+ const uint8_t* ipv6_begin = static_cast<const uint8_t*>(ipv6);
16+ const uint8_t* ipv6_end = ipv6_begin + ADDR_IPV6_SIZE;
17
18- if (memcmp(ipv6, IPV4_IN_IPV6_PREFIX, sizeof(IPV4_IN_IPV6_PREFIX)) == 0) {
19+ if (memcmp(ipv6_begin, IPV4_IN_IPV6_PREFIX, sizeof(IPV4_IN_IPV6_PREFIX)) == 0) {
20 // IPv4-in-IPv6
21 m_net = NET_IPV4;
22- m_addr.assign(ipv6 + sizeof(IPV4_IN_IPV6_PREFIX), ipv6_end);
23- } else if (memcmp(ipv6, TORV2_IN_IPV6_PREFIX, sizeof(TORV2_IN_IPV6_PREFIX)) == 0) {
24+ m_addr.assign(ipv6_begin + sizeof(IPV4_IN_IPV6_PREFIX), ipv6_end);
25+ } else if (memcmp(ipv6_begin, TORV2_IN_IPV6_PREFIX, sizeof(TORV2_IN_IPV6_PREFIX)) == 0) {
26 // TORv2-in-IPv6
27 m_net = NET_ONION;
28- m_addr.assign(ipv6 + sizeof(TORV2_IN_IPV6_PREFIX), ipv6_end);
29- } else if (memcmp(ipv6, INTERNAL_IN_IPV6_PREFIX, sizeof(INTERNAL_IN_IPV6_PREFIX)) == 0) {
30+ m_addr.assign(ipv6_begin + sizeof(TORV2_IN_IPV6_PREFIX), ipv6_end);
31+ } else if (memcmp(ipv6_begin, INTERNAL_IN_IPV6_PREFIX, sizeof(INTERNAL_IN_IPV6_PREFIX)) == 0) {
32 // Internal-in-IPv6
33 m_net = NET_INTERNAL;
34- m_addr.assign(ipv6 + sizeof(INTERNAL_IN_IPV6_PREFIX), ipv6_end);
35+ m_addr.assign(ipv6_begin + sizeof(INTERNAL_IN_IPV6_PREFIX), ipv6_end);
36 } else {
37 // IPv6
38 m_net = NET_IPV6;
39- m_addr.assign(ipv6, ipv6_end);
40+ m_addr.assign(ipv6_begin, ipv6_end);
41 }
42 }
43
44 /**
45 * Try to make this a dummy address that maps the specified name into IPv6 like
46 * so: (0xFD + %sha256("bitcoin")[0:5]) + %sha256(name)[0:10]. Such dummy
47@@ -105,13 +106,13 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
48 m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
49 }
50
51 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
52 {
53 assert(sizeof(ipv6Addr) == ADDR_IPV6_SIZE);
54- SetLegacyIPv6(*reinterpret_cast<const uint8_t (*)[ADDR_IPV6_SIZE]>(&ipv6Addr));
55+ SetLegacyIPv6(&ipv6Addr);
56 scopeId = scope;
57 }
58
59 bool CNetAddr::IsBindAny() const
60 {
61 if (!IsIPv4() && !IsIPv6()) {
62diff --git i/src/netaddress.h w/src/netaddress.h
63index 8924ed0b4..ca048fbf5 100644
64--- i/src/netaddress.h
65+++ w/src/netaddress.h
66@@ -105,13 +105,13 @@ class CNetAddr
67 /**
68 * Set from a legacy IPv6 address.
69 * Legacy IPv6 address may be a normal IPv6 address, or another address
70 * (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy
71 * `addr` encoding.
72 */
73- void SetLegacyIPv6(const uint8_t (&ipv6)[ADDR_IPV6_SIZE]);
74+ void SetLegacyIPv6(const void* ipv6);
75
76 bool SetInternal(const std::string& name);
77
78 bool SetSpecial(const std::string &strName); // for Tor addresses
79 bool IsBindAny() const; // INADDR_ANY equivalent
80 bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
81diff --git i/src/test/fuzz/asmap.cpp w/src/test/fuzz/asmap.cpp
82index 84902ee2d..98ac82453 100644
83--- i/src/test/fuzz/asmap.cpp
84+++ w/src/test/fuzz/asmap.cpp
85@@ -45,13 +45,13 @@ void test_one_input(const std::vector<uint8_t>& buffer)
86 if (!SanityCheckASMap(asmap)) return;
87
88 const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
89 CNetAddr net_addr;
90 if (ipv6) {
91 assert(addr_size == ADDR_IPV6_SIZE);
92- net_addr.SetLegacyIPv6(*reinterpret_cast<const uint8_t (*)[ADDR_IPV6_SIZE]>(addr_data));
93+ net_addr.SetLegacyIPv6(addr_data);
94 } else {
95 assert(addr_size == ADDR_IPV4_SIZE);
96 in_addr ipv4;
97 memcpy(&ipv4, addr_data, addr_size);
98 net_addr.SetIP(CNetAddr{ipv4});
99 }
0diff --git i/src/netaddress.cpp w/src/netaddress.cpp
1index b421e1b11..121b92650 100644
2--- i/src/netaddress.cpp
3+++ w/src/netaddress.cpp
4@@ -26,32 +26,32 @@ CNetAddr::CNetAddr() {}
5 void CNetAddr::SetIP(const CNetAddr& ipIn)
6 {
7 m_net = ipIn.m_net;
8 m_addr = ipIn.m_addr;
9 }
10
11-void CNetAddr::SetLegacyIPv6(const uint8_t (&ipv6)[ADDR_IPV6_SIZE])
12+void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
13 {
14- const uint8_t* ipv6_end = ipv6 + ADDR_IPV6_SIZE;
15+ assert(ipv6.size() == ADDR_IPV6_SIZE);
16
17- if (memcmp(ipv6, IPV4_IN_IPV6_PREFIX, sizeof(IPV4_IN_IPV6_PREFIX)) == 0) {
18+ if (memcmp(ipv6.begin(), IPV4_IN_IPV6_PREFIX, sizeof(IPV4_IN_IPV6_PREFIX)) == 0) {
19 // IPv4-in-IPv6
20 m_net = NET_IPV4;
21- m_addr.assign(ipv6 + sizeof(IPV4_IN_IPV6_PREFIX), ipv6_end);
22- } else if (memcmp(ipv6, TORV2_IN_IPV6_PREFIX, sizeof(TORV2_IN_IPV6_PREFIX)) == 0) {
23+ m_addr.assign(ipv6.begin() + sizeof(IPV4_IN_IPV6_PREFIX), ipv6.end());
24+ } else if (memcmp(ipv6.begin(), TORV2_IN_IPV6_PREFIX, sizeof(TORV2_IN_IPV6_PREFIX)) == 0) {
25 // TORv2-in-IPv6
26 m_net = NET_ONION;
27- m_addr.assign(ipv6 + sizeof(TORV2_IN_IPV6_PREFIX), ipv6_end);
28- } else if (memcmp(ipv6, INTERNAL_IN_IPV6_PREFIX, sizeof(INTERNAL_IN_IPV6_PREFIX)) == 0) {
29+ m_addr.assign(ipv6.begin() + sizeof(TORV2_IN_IPV6_PREFIX), ipv6.end());
30+ } else if (memcmp(ipv6.begin(), INTERNAL_IN_IPV6_PREFIX, sizeof(INTERNAL_IN_IPV6_PREFIX)) == 0) {
31 // Internal-in-IPv6
32 m_net = NET_INTERNAL;
33- m_addr.assign(ipv6 + sizeof(INTERNAL_IN_IPV6_PREFIX), ipv6_end);
34+ m_addr.assign(ipv6.begin() + sizeof(INTERNAL_IN_IPV6_PREFIX), ipv6.end());
35 } else {
36 // IPv6
37 m_net = NET_IPV6;
38- m_addr.assign(ipv6, ipv6_end);
39+ m_addr.assign(ipv6.begin(), ipv6.end());
40 }
41 }
42
43 /**
44 * Try to make this a dummy address that maps the specified name into IPv6 like
45 * so: (0xFD + %sha256("bitcoin")[0:5]) + %sha256(name)[0:10]. Such dummy
46@@ -104,14 +104,13 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
47 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
48 m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
49 }
50
51 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
52 {
53- assert(sizeof(ipv6Addr) == ADDR_IPV6_SIZE);
54- SetLegacyIPv6(*reinterpret_cast<const uint8_t (*)[ADDR_IPV6_SIZE]>(&ipv6Addr));
55+ SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
56 scopeId = scope;
57 }
58
59 bool CNetAddr::IsBindAny() const
60 {
61 if (!IsIPv4() && !IsIPv6()) {
62diff --git i/src/netaddress.h w/src/netaddress.h
63index 8924ed0b4..a76b5e9ce 100644
64--- i/src/netaddress.h
65+++ w/src/netaddress.h
66@@ -105,13 +105,13 @@ class CNetAddr
67 /**
68 * Set from a legacy IPv6 address.
69 * Legacy IPv6 address may be a normal IPv6 address, or another address
70 * (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy
71 * `addr` encoding.
72 */
73- void SetLegacyIPv6(const uint8_t (&ipv6)[ADDR_IPV6_SIZE]);
74+ void SetLegacyIPv6(Span<const uint8_t> ipv6);
75
76 bool SetInternal(const std::string& name);
77
78 bool SetSpecial(const std::string &strName); // for Tor addresses
79 bool IsBindAny() const; // INADDR_ANY equivalent
80 bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
81diff --git i/src/test/fuzz/asmap.cpp w/src/test/fuzz/asmap.cpp
82index 84902ee2d..e3aefa18a 100644
83--- i/src/test/fuzz/asmap.cpp
84+++ w/src/test/fuzz/asmap.cpp
85@@ -45,13 +45,13 @@ void test_one_input(const std::vector<uint8_t>& buffer)
86 if (!SanityCheckASMap(asmap)) return;
87
88 const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
89 CNetAddr net_addr;
90 if (ipv6) {
91 assert(addr_size == ADDR_IPV6_SIZE);
92- net_addr.SetLegacyIPv6(*reinterpret_cast<const uint8_t (*)[ADDR_IPV6_SIZE]>(addr_data));
93+ net_addr.SetLegacyIPv6(Span<const uint8_t>(addr_data, addr_size));
94 } else {
95 assert(addr_size == ADDR_IPV4_SIZE);
96 in_addr ipv4;
97 memcpy(&ipv4, addr_data, addr_size);
98 net_addr.SetIP(CNetAddr{ipv4});
99 }
What do you think?