aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/protocol.cpp9
-rw-r--r--src/protocol.h4
2 files changed, 11 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);
}
diff --git a/src/protocol.h b/src/protocol.h
index db07efb9f9..6639ae2aac 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -37,6 +37,10 @@ public:
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
explicit CMessageHeader(const MessageStartChars& pchMessageStartIn);
+
+ /** 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.
+ */
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
std::string GetCommand() const;