Skip to content

Commit 1b75f25

Browse files
committed
Merge #20432: net: Treat raw message bytes as uint8_t
fabecce net: Treat raw message bytes as uint8_t (MarcoFalke) Pull request description: Using `uint8_t` from the beginning when messages are `recv`ed has two style benefits: * The signedness is clear from reading the code, as it does not depend on the architecture * When passing the bytes on, the need for static signedness casts is dropped, making the code a bit less verbose and more coherent ACKs for top commit: laanwj: Code review ACK fabecce theStack: Code Review ACK fabecce jonatack: Tested ACK fabecce Tree-SHA512: e6d9803c78633fde3304faf592afa961ff9462a7912d1da97a24720265274aa10ab4168d71b6ec2756b7448dd42585321afee0e5c889e705be778ce9a330d145
2 parents e9a1c9f + fabecce commit 1b75f25

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

src/net.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
629629
}
630630
#undef X
631631

632-
bool CNode::ReceiveMsgBytes(Span<const char> msg_bytes, bool& complete)
632+
bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
633633
{
634634
complete = false;
635635
const auto time = GetTime<std::chrono::microseconds>();
@@ -673,7 +673,7 @@ bool CNode::ReceiveMsgBytes(Span<const char> msg_bytes, bool& complete)
673673
return true;
674674
}
675675

676-
int V1TransportDeserializer::readHeader(Span<const char> msg_bytes)
676+
int V1TransportDeserializer::readHeader(Span<const uint8_t> msg_bytes)
677677
{
678678
// copy data to temporary parsing buffer
679679
unsigned int nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos;
@@ -713,7 +713,7 @@ int V1TransportDeserializer::readHeader(Span<const char> msg_bytes)
713713
return nCopy;
714714
}
715715

716-
int V1TransportDeserializer::readData(Span<const char> msg_bytes)
716+
int V1TransportDeserializer::readData(Span<const uint8_t> msg_bytes)
717717
{
718718
unsigned int nRemaining = hdr.nMessageSize - nDataPos;
719719
unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
@@ -723,7 +723,7 @@ int V1TransportDeserializer::readData(Span<const char> msg_bytes)
723723
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
724724
}
725725

726-
hasher.Write(MakeUCharSpan(msg_bytes.first(nCopy)));
726+
hasher.Write(msg_bytes.first(nCopy));
727727
memcpy(&vRecv[nDataPos], msg_bytes.data(), nCopy);
728728
nDataPos += nCopy;
729729

@@ -1463,18 +1463,18 @@ void CConnman::SocketHandler()
14631463
if (recvSet || errorSet)
14641464
{
14651465
// typical socket buffer is 8K-64K
1466-
char pchBuf[0x10000];
1466+
uint8_t pchBuf[0x10000];
14671467
int nBytes = 0;
14681468
{
14691469
LOCK(pnode->cs_hSocket);
14701470
if (pnode->hSocket == INVALID_SOCKET)
14711471
continue;
1472-
nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1472+
nBytes = recv(pnode->hSocket, (char*)pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
14731473
}
14741474
if (nBytes > 0)
14751475
{
14761476
bool notify = false;
1477-
if (!pnode->ReceiveMsgBytes(Span<const char>(pchBuf, nBytes), notify))
1477+
if (!pnode->ReceiveMsgBytes(Span<const uint8_t>(pchBuf, nBytes), notify))
14781478
pnode->CloseSocketDisconnect();
14791479
RecordBytesRecv(nBytes);
14801480
if (notify) {

src/net.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ class TransportDeserializer {
758758
// set the serialization context version
759759
virtual void SetVersion(int version) = 0;
760760
/** read and deserialize data, advances msg_bytes data pointer */
761-
virtual int Read(Span<const char>& msg_bytes) = 0;
761+
virtual int Read(Span<const uint8_t>& msg_bytes) = 0;
762762
// decomposes a message from the context
763763
virtual Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err) = 0;
764764
virtual ~TransportDeserializer() {}
@@ -779,8 +779,8 @@ class V1TransportDeserializer final : public TransportDeserializer
779779
unsigned int nDataPos;
780780

781781
const uint256& GetMessageHash() const;
782-
int readHeader(Span<const char> msg_bytes);
783-
int readData(Span<const char> msg_bytes);
782+
int readHeader(Span<const uint8_t> msg_bytes);
783+
int readData(Span<const uint8_t> msg_bytes);
784784

785785
void Reset() {
786786
vRecv.clear();
@@ -814,7 +814,7 @@ class V1TransportDeserializer final : public TransportDeserializer
814814
hdrbuf.SetVersion(nVersionIn);
815815
vRecv.SetVersion(nVersionIn);
816816
}
817-
int Read(Span<const char>& msg_bytes) override
817+
int Read(Span<const uint8_t>& msg_bytes) override
818818
{
819819
int ret = in_data ? readData(msg_bytes) : readHeader(msg_bytes);
820820
if (ret < 0) {
@@ -1132,7 +1132,7 @@ class CNode
11321132
* @return True if the peer should stay connected,
11331133
* False if the peer should be disconnected from.
11341134
*/
1135-
bool ReceiveMsgBytes(Span<const char> msg_bytes, bool& complete);
1135+
bool ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete);
11361136

11371137
void SetCommonVersion(int greatest_common_version)
11381138
{

src/test/fuzz/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
128128
case 11: {
129129
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
130130
bool complete;
131-
node.ReceiveMsgBytes({(const char*)b.data(), b.size()}, complete);
131+
node.ReceiveMsgBytes(b, complete);
132132
break;
133133
}
134134
}

src/test/fuzz/p2p_transport_deserializer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
2121
{
2222
// Construct deserializer, with a dummy NodeId
2323
V1TransportDeserializer deserializer{Params(), (NodeId)0, SER_NETWORK, INIT_PROTO_VERSION};
24-
Span<const char> msg_bytes{(const char*)buffer.data(), buffer.size()};
24+
Span<const uint8_t> msg_bytes{buffer};
2525
while (msg_bytes.size() > 0) {
2626
const int handled = deserializer.Read(msg_bytes);
2727
if (handled < 0) {

src/test/util/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <chainparams.h>
88
#include <net.h>
99

10-
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const char> msg_bytes, bool& complete) const
10+
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const
1111
{
1212
assert(node.ReceiveMsgBytes(msg_bytes, complete));
1313
if (complete) {
@@ -29,11 +29,11 @@ void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const char> msg_bytes
2929

3030
bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const
3131
{
32-
std::vector<unsigned char> ser_msg_header;
32+
std::vector<uint8_t> ser_msg_header;
3333
node.m_serializer->prepareForTransport(ser_msg, ser_msg_header);
3434

3535
bool complete;
36-
NodeReceiveMsgBytes(node, {(const char*)ser_msg_header.data(), ser_msg_header.size()}, complete);
37-
NodeReceiveMsgBytes(node, {(const char*)ser_msg.data.data(), ser_msg.data.size()}, complete);
36+
NodeReceiveMsgBytes(node, ser_msg_header, complete);
37+
NodeReceiveMsgBytes(node, ser_msg.data, complete);
3838
return complete;
3939
}

src/test/util/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ConnmanTestMsg : public CConnman {
2525

2626
void ProcessMessagesOnce(CNode& node) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }
2727

28-
void NodeReceiveMsgBytes(CNode& node, Span<const char> msg_bytes, bool& complete) const;
28+
void NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const;
2929

3030
bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
3131
};

0 commit comments

Comments
 (0)