refactor: Replace memset calls with array initialization

This commit is contained in:
João Barbosa 2021-05-13 08:44:34 +01:00
parent 9c891b64ff
commit 1c9255c7dd
2 changed files with 5 additions and 14 deletions

View File

@ -87,25 +87,16 @@ const static std::string allNetMessageTypes[] = {
}; };
const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes)); const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes));
CMessageHeader::CMessageHeader()
{
memset(pchMessageStart, 0, MESSAGE_START_SIZE);
memset(pchCommand, 0, sizeof(pchCommand));
memset(pchChecksum, 0, CHECKSUM_SIZE);
}
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn) CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
{ {
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE); memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
// Copy the command name, zero-padding to COMMAND_SIZE bytes // Copy the command name
size_t i = 0; size_t i = 0;
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i]; for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;
nMessageSize = nMessageSizeIn; nMessageSize = nMessageSizeIn;
memset(pchChecksum, 0, CHECKSUM_SIZE);
} }
std::string CMessageHeader::GetCommand() const std::string CMessageHeader::GetCommand() const

View File

@ -38,7 +38,7 @@ public:
static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE; static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE]; typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
explicit CMessageHeader(); explicit CMessageHeader() = default;
/** Construct a P2P message header from message-start characters, a command and the size of the message. /** Construct a P2P message header from message-start characters, a command and the size of the message.
* @note Passing in a `pszCommand` longer than COMMAND_SIZE will result in a run-time assertion error. * @note Passing in a `pszCommand` longer than COMMAND_SIZE will result in a run-time assertion error.
@ -50,10 +50,10 @@ public:
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); } SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
char pchMessageStart[MESSAGE_START_SIZE]; char pchMessageStart[MESSAGE_START_SIZE]{};
char pchCommand[COMMAND_SIZE]; char pchCommand[COMMAND_SIZE]{};
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()}; uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
uint8_t pchChecksum[CHECKSUM_SIZE]; uint8_t pchChecksum[CHECKSUM_SIZE]{};
}; };
/** /**