Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace memset calls with array initialization #2452

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,16 @@ static const char* ppszTypeName[] =
"scraperindex",
};

CMessageHeader::CMessageHeader()
{
memcpy(pchMessageStart, Params().MessageStart(), CMessageHeader::MESSAGE_START_SIZE);
memset(pchCommand, 0, sizeof(pchCommand));
memset(pchChecksum, 0, CHECKSUM_SIZE);
}

CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
{
memcpy(pchMessageStart, Params().MessageStart(), CMessageHeader::MESSAGE_START_SIZE);

// Copy the command name, zero-padding to COMMAND_SIZE bytes
// Copy the command name
size_t i = 0;
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
for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;

nMessageSize = nMessageSizeIn;
memset(pchChecksum, 0, CHECKSUM_SIZE);
}

std::string CMessageHeader::GetCommand() const
Expand Down
8 changes: 4 additions & 4 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ class CMessageHeader
static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];

CMessageHeader();
CMessageHeader() = default;
CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);

std::string GetCommand() const;
bool IsValid() const;

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

char pchMessageStart[MESSAGE_START_SIZE];
char pchCommand[COMMAND_SIZE];
char pchMessageStart[MESSAGE_START_SIZE]{};
char pchCommand[COMMAND_SIZE]{};
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
uint8_t pchChecksum[CHECKSUM_SIZE];
uint8_t pchChecksum[CHECKSUM_SIZE]{};
};

/** nServices flags */
Expand Down