Skip to content

Commit b34bf2b

Browse files
committed
Merge bitcoin#21939: refactor: Replace memset calls with array initialization
1c9255c refactor: Replace memset calls with array initialization (João Barbosa) Pull request description: Follow up to bitcoin#21905 (review). ACKs for top commit: laanwj: re-ACK 1c9255c Crypt-iQ: Code review ACK 1c9255c Tree-SHA512: 4b61dec2094f4781ef1c0427ee3bda3cfea12111274eebc7bc40a84f261d9c1681dd0860c57200bea2456588e44e8e0aecd18545c25f1f1250dd331ab7d05f28
2 parents 4741aec + 1c9255c commit b34bf2b

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
lines changed

src/protocol.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,16 @@ const static std::string allNetMessageTypes[] = {
8787
};
8888
const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes));
8989

90-
CMessageHeader::CMessageHeader()
91-
{
92-
memset(pchMessageStart, 0, MESSAGE_START_SIZE);
93-
memset(pchCommand, 0, sizeof(pchCommand));
94-
memset(pchChecksum, 0, CHECKSUM_SIZE);
95-
}
96-
9790
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
9891
{
9992
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
10093

101-
// Copy the command name, zero-padding to COMMAND_SIZE bytes
94+
// Copy the command name
10295
size_t i = 0;
10396
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
10497
assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
105-
for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;
10698

10799
nMessageSize = nMessageSizeIn;
108-
memset(pchChecksum, 0, CHECKSUM_SIZE);
109100
}
110101

111102
std::string CMessageHeader::GetCommand() const

src/protocol.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CMessageHeader
3838
static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
3939
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
4040

41-
explicit CMessageHeader();
41+
explicit CMessageHeader() = default;
4242

4343
/** Construct a P2P message header from message-start characters, a command and the size of the message.
4444
* @note Passing in a `pszCommand` longer than COMMAND_SIZE will result in a run-time assertion error.
@@ -50,10 +50,10 @@ class CMessageHeader
5050

5151
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
5252

53-
char pchMessageStart[MESSAGE_START_SIZE];
54-
char pchCommand[COMMAND_SIZE];
53+
char pchMessageStart[MESSAGE_START_SIZE]{};
54+
char pchCommand[COMMAND_SIZE]{};
5555
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
56-
uint8_t pchChecksum[CHECKSUM_SIZE];
56+
uint8_t pchChecksum[CHECKSUM_SIZE]{};
5757
};
5858

5959
/**

0 commit comments

Comments
 (0)