Skip to content

Commit c754dfe

Browse files
MarcoFalkePiRK
authored andcommitted
net: Treat raw message bytes as uint8_t
Summary: This is a backport of [[bitcoin/bitcoin#20432 | core#20432]] Test Plan: `ninja all check-all bitcoin-fuzzers` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D11090
1 parent c278bdf commit c754dfe

File tree

6 files changed

+19
-24
lines changed

6 files changed

+19
-24
lines changed

src/net.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ void CNode::copyStats(CNodeStats &stats) {
656656
stats.m_conn_type_string = ConnectionTypeAsString();
657657
}
658658

659-
bool CNode::ReceiveMsgBytes(const Config &config, Span<const char> msg_bytes,
659+
bool CNode::ReceiveMsgBytes(const Config &config, Span<const uint8_t> msg_bytes,
660660
bool &complete) {
661661
complete = false;
662662
const auto time = GetTime<std::chrono::microseconds>();
@@ -696,7 +696,7 @@ bool CNode::ReceiveMsgBytes(const Config &config, Span<const char> msg_bytes,
696696
}
697697

698698
int V1TransportDeserializer::readHeader(const Config &config,
699-
Span<const char> msg_bytes) {
699+
Span<const uint8_t> msg_bytes) {
700700
// copy data to temporary parsing buffer
701701
uint32_t nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos;
702702
uint32_t nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
@@ -728,7 +728,7 @@ int V1TransportDeserializer::readHeader(const Config &config,
728728
return nCopy;
729729
}
730730

731-
int V1TransportDeserializer::readData(Span<const char> msg_bytes) {
731+
int V1TransportDeserializer::readData(Span<const uint8_t> msg_bytes) {
732732
unsigned int nRemaining = hdr.nMessageSize - nDataPos;
733733
unsigned int nCopy = std::min<unsigned int>(nRemaining, msg_bytes.size());
734734

@@ -738,7 +738,7 @@ int V1TransportDeserializer::readData(Span<const char> msg_bytes) {
738738
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
739739
}
740740

741-
hasher.Write(MakeUCharSpan(msg_bytes.first(nCopy)));
741+
hasher.Write(msg_bytes.first(nCopy));
742742
memcpy(&vRecv[nDataPos], msg_bytes.data(), nCopy);
743743
nDataPos += nCopy;
744744

@@ -1819,20 +1819,20 @@ void CConnman::SocketHandler() {
18191819
}
18201820
if (recvSet || errorSet) {
18211821
// typical socket buffer is 8K-64K
1822-
char pchBuf[0x10000];
1822+
uint8_t pchBuf[0x10000];
18231823
int32_t nBytes = 0;
18241824
{
18251825
LOCK(pnode->cs_hSocket);
18261826
if (pnode->hSocket == INVALID_SOCKET) {
18271827
continue;
18281828
}
1829-
nBytes =
1830-
recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1829+
nBytes = recv(pnode->hSocket, (char *)pchBuf, sizeof(pchBuf),
1830+
MSG_DONTWAIT);
18311831
}
18321832
if (nBytes > 0) {
18331833
bool notify = false;
18341834
if (!pnode->ReceiveMsgBytes(
1835-
*config, Span<const char>(pchBuf, nBytes), notify)) {
1835+
*config, Span<const uint8_t>(pchBuf, nBytes), notify)) {
18361836
pnode->CloseSocketDisconnect();
18371837
}
18381838
RecordBytesRecv(nBytes);

src/net.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class TransportDeserializer {
357357
// set the serialization context version
358358
virtual void SetVersion(int version) = 0;
359359
/** read and deserialize data, advances msg_bytes data pointer */
360-
virtual int Read(const Config &config, Span<const char> &msg_bytes) = 0;
360+
virtual int Read(const Config &config, Span<const uint8_t> &msg_bytes) = 0;
361361
// decomposes a message from the context
362362
virtual CNetMessage GetMessage(const Config &config,
363363
std::chrono::microseconds time) = 0;
@@ -381,8 +381,8 @@ class V1TransportDeserializer final : public TransportDeserializer {
381381
uint32_t nDataPos;
382382

383383
const uint256 &GetMessageHash() const;
384-
int readHeader(const Config &config, Span<const char> msg_bytes);
385-
int readData(Span<const char> msg_bytes);
384+
int readHeader(const Config &config, Span<const uint8_t> msg_bytes);
385+
int readData(Span<const uint8_t> msg_bytes);
386386

387387
void Reset() {
388388
vRecv.clear();
@@ -416,7 +416,7 @@ class V1TransportDeserializer final : public TransportDeserializer {
416416
hdrbuf.SetVersion(nVersionIn);
417417
vRecv.SetVersion(nVersionIn);
418418
}
419-
int Read(const Config &config, Span<const char> &msg_bytes) override {
419+
int Read(const Config &config, Span<const uint8_t> &msg_bytes) override {
420420
int ret = in_data ? readData(msg_bytes) : readHeader(config, msg_bytes);
421421
if (ret < 0) {
422422
Reset();
@@ -815,7 +815,7 @@ class CNode {
815815
* @return True if the peer should stay connected,
816816
* False if the peer should be disconnected from.
817817
*/
818-
bool ReceiveMsgBytes(const Config &config, Span<const char> msg_bytes,
818+
bool ReceiveMsgBytes(const Config &config, Span<const uint8_t> msg_bytes,
819819
bool &complete);
820820

821821
void SetCommonVersion(int greatest_common_version) {

src/test/fuzz/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ void test_one_input(const std::vector<uint8_t> &buffer) {
113113
const std::vector<uint8_t> b =
114114
ConsumeRandomLengthByteVector(fuzzed_data_provider);
115115
bool complete;
116-
node.ReceiveMsgBytes(config, {(const char *)b.data(), b.size()},
117-
complete);
116+
node.ReceiveMsgBytes(config, b, complete);
118117
break;
119118
}
120119
}

src/test/fuzz/p2p_transport_deserializer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void test_one_input(const std::vector<uint8_t> &buffer) {
2222
const Config &config = GetConfig();
2323
V1TransportDeserializer deserializer{config.GetChainParams().NetMagic(),
2424
SER_NETWORK, INIT_PROTO_VERSION};
25-
Span<const char> msg_bytes{(const char *)buffer.data(), buffer.size()};
25+
Span<const uint8_t> msg_bytes{buffer};
2626
while (msg_bytes.size() > 0) {
2727
const int handled = deserializer.Read(config, msg_bytes);
2828
if (handled < 0) {

src/test/util/net.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <vector>
1313

1414
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode &node,
15-
Span<const char> msg_bytes,
15+
Span<const uint8_t> msg_bytes,
1616
bool &complete) const {
1717
assert(node.ReceiveMsgBytes(*config, msg_bytes, complete));
1818
if (complete) {
@@ -40,12 +40,8 @@ bool ConnmanTestMsg::ReceiveMsgFrom(CNode &node,
4040
node.m_serializer->prepareForTransport(*config, ser_msg, ser_msg_header);
4141

4242
bool complete;
43-
NodeReceiveMsgBytes(
44-
node, {(const char *)ser_msg_header.data(), ser_msg_header.size()},
45-
complete);
46-
NodeReceiveMsgBytes(
47-
node, {(const char *)ser_msg.data.data(), ser_msg.data.size()},
48-
complete);
43+
NodeReceiveMsgBytes(node, ser_msg_header, complete);
44+
NodeReceiveMsgBytes(node, ser_msg.data, complete);
4945
return complete;
5046
}
5147

src/test/util/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct ConnmanTestMsg : public CConnman {
3838
m_msgproc->ProcessMessages(*config, &node, flagInterruptMsgProc);
3939
}
4040

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

4444
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const;

0 commit comments

Comments
 (0)