aboutsummaryrefslogtreecommitdiff
path: root/src/protocol.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@protonmail.com>2019-09-30 12:27:27 +0200
committerWladimir J. van der Laan <laanwj@protonmail.com>2020-02-06 13:24:40 +0100
commitb837b334db5dd6232725fd2350928ff4fbd3feee (patch)
treea20d984e931bae8b2a7272977056743b25b19d3e /src/protocol.cpp
parent8a56f79d491271120abc3843c46e9dda44edd308 (diff)
net: Fail instead of truncate command name in CMessageHeader
Replace the memset/strncpy dance in `CMessageHeader::CMessageHeader` with explicit code that copies then name and asserts the length. This removes a warning in g++ 9.1.1 and IMO makes the code more readable by not relying on strncpy padding and silent truncation behavior.
Diffstat (limited to 'src/protocol.cpp')
-rw-r--r--src/protocol.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/protocol.cpp b/src/protocol.cpp
index e49e5523ac..bd3ed25a8a 100644
--- a/src/protocol.cpp
+++ b/src/protocol.cpp
@@ -85,8 +85,13 @@ CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
{
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
- memset(pchCommand, 0, sizeof(pchCommand));
- strncpy(pchCommand, pszCommand, COMMAND_SIZE);
+
+ // Copy the command name, zero-padding to COMMAND_SIZE bytes
+ 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);
}