diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -755,7 +755,7 @@ // Store received bytes per message command to prevent a memory DOS, // only allow valid commands. mapMsgCmdSize::iterator i = - mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand); + mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand.data()); if (i == mapRecvBytesPerMsgCmd.end()) { i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER); } diff --git a/src/protocol.h b/src/protocol.h --- a/src/protocol.h +++ b/src/protocol.h @@ -69,7 +69,7 @@ } MessageMagic pchMessageStart; - char pchCommand[COMMAND_SIZE]; + std::array pchCommand; uint32_t nMessageSize; uint8_t pchChecksum[CHECKSUM_SIZE]; }; diff --git a/src/protocol.cpp b/src/protocol.cpp --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -71,7 +71,7 @@ CMessageHeader::CMessageHeader(const MessageMagic &pchMessageStartIn) { memcpy(std::begin(pchMessageStart), std::begin(pchMessageStartIn), MESSAGE_START_SIZE); - memset(pchCommand, 0, sizeof(pchCommand)); + memset(pchCommand.data(), 0, sizeof(pchCommand)); nMessageSize = -1; memset(pchChecksum, 0, CHECKSUM_SIZE); } @@ -81,15 +81,17 @@ unsigned int nMessageSizeIn) { memcpy(std::begin(pchMessageStart), std::begin(pchMessageStartIn), MESSAGE_START_SIZE); - memset(pchCommand, 0, sizeof(pchCommand)); - strncpy(pchCommand, pszCommand, COMMAND_SIZE); + memset(pchCommand.data(), 0, sizeof(pchCommand)); + strncpy(pchCommand.data(), pszCommand, COMMAND_SIZE); nMessageSize = nMessageSizeIn; memset(pchChecksum, 0, CHECKSUM_SIZE); } std::string CMessageHeader::GetCommand() const { - return std::string(pchCommand, - pchCommand + strnlen(pchCommand, COMMAND_SIZE)); + // return std::string(pchCommand.begin(), pchCommand.end()); + return std::string(pchCommand.data(), + pchCommand.data() + + strnlen(pchCommand.data(), COMMAND_SIZE)); } static bool @@ -102,11 +104,11 @@ } // Check the command string for errors - for (const char *p1 = header.pchCommand; - p1 < header.pchCommand + CMessageHeader::COMMAND_SIZE; p1++) { + for (const char *p1 = header.pchCommand.data(); + p1 < header.pchCommand.data() + CMessageHeader::COMMAND_SIZE; p1++) { if (*p1 == 0) { // Must be all zeros after the first zero - for (; p1 < header.pchCommand + CMessageHeader::COMMAND_SIZE; + for (; p1 < header.pchCommand.data() + CMessageHeader::COMMAND_SIZE; p1++) { if (*p1 != 0) { return false;