Skip to content

Commit 8eda721

Browse files
committed
Merge bitcoin#33743: fuzz: refactor memcpy to std::ranges::copy to work around ubsan warn
fa4b52b fuzz: refactor memcpy to std::ranges::copy to work around ubsan warn (MarcoFalke) Pull request description: Using std::ranges::copy from the C++ standard library has a few benefits here: * It has the additional benefit of being a bit more type safe and document the byte cast explicitly. * The compiler will likely optimize it to the same asm, but performance doesn't really matter here anyway. * It has defined semantics for empty source ranges. Fixes bitcoin#33643 ACKs for top commit: marcofleon: tACK fa4b52b dergoegge: utACK fa4b52b Tree-SHA512: 04fcf096e3cfc526e996c9313ec6e0a4d12c382fa19cb846b51564d33de2f0ef78a588fc6a936da0c76ca8bc9d9db4a824c36d99413db4f538a98239864d48f0
2 parents 6f35969 + fa4b52b commit 8eda721

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/test/fuzz/util/net.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2009-2022 The Bitcoin Core developers
1+
// Copyright (c) 2009-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -20,6 +20,7 @@
2020
#include <cstdint>
2121
#include <cstdlib>
2222
#include <cstring>
23+
#include <ranges>
2324
#include <thread>
2425
#include <vector>
2526

@@ -322,8 +323,8 @@ std::unique_ptr<Sock> FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) co
322323
*addr_len = write_len;
323324
auto addr4 = reinterpret_cast<sockaddr_in*>(addr);
324325
addr4->sin_family = AF_INET;
325-
const auto sin_addr_bytes = m_fuzzed_data_provider.ConsumeBytes<uint8_t>(sizeof(addr4->sin_addr));
326-
memcpy(&addr4->sin_addr, sin_addr_bytes.data(), sin_addr_bytes.size());
326+
const auto sin_addr_bytes{m_fuzzed_data_provider.ConsumeBytes<std::byte>(sizeof(addr4->sin_addr))};
327+
std::ranges::copy(sin_addr_bytes, reinterpret_cast<std::byte*>(&addr4->sin_addr));
327328
addr4->sin_port = m_fuzzed_data_provider.ConsumeIntegralInRange<uint16_t>(1, 65535);
328329
}
329330
} else {
@@ -333,8 +334,8 @@ std::unique_ptr<Sock> FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) co
333334
*addr_len = write_len;
334335
auto addr6 = reinterpret_cast<sockaddr_in6*>(addr);
335336
addr6->sin6_family = AF_INET6;
336-
const auto sin_addr_bytes = m_fuzzed_data_provider.ConsumeBytes<uint8_t>(sizeof(addr6->sin6_addr));
337-
memcpy(&addr6->sin6_addr, sin_addr_bytes.data(), sin_addr_bytes.size());
337+
const auto sin_addr_bytes{m_fuzzed_data_provider.ConsumeBytes<std::byte>(sizeof(addr6->sin6_addr))};
338+
std::ranges::copy(sin_addr_bytes, reinterpret_cast<std::byte*>(&addr6->sin6_addr));
338339
addr6->sin6_port = m_fuzzed_data_provider.ConsumeIntegralInRange<uint16_t>(1, 65535);
339340
}
340341
}

0 commit comments

Comments
 (0)