From b837b334db5dd6232725fd2350928ff4fbd3feee Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 30 Sep 2019 12:27:27 +0200 Subject: [PATCH 1/2] 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. --- src/protocol.cpp | 9 +++++++-- src/protocol.h | 4 ++++ 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; From ff9c671b11d40e5d0623eff3dd12e48cbaafb34e Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Tue, 1 Oct 2019 14:00:46 +0200 Subject: [PATCH 2/2] refactor: Work around GCC 9 `-Wredundant-move` warning Use std::move workaround for unique_ptr, for when the C++ compiler lacks a fix for this issue: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1579 Do this in a way that avoids a GCC 9 `-Wredundant-move` warning. --- src/interfaces/chain.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 643bb58d56..5a3420349f 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -236,11 +236,10 @@ public: explicit ChainImpl(NodeContext& node) : m_node(node) {} std::unique_ptr lock(bool try_lock) override { - auto result = MakeUnique(::cs_main, "cs_main", __FILE__, __LINE__, try_lock); - if (try_lock && result && !*result) return {}; - // std::move necessary on some compilers due to conversion from - // LockImpl to Lock pointer - return std::move(result); + auto lock = MakeUnique(::cs_main, "cs_main", __FILE__, __LINE__, try_lock); + if (try_lock && lock && !*lock) return {}; + std::unique_ptr result = std::move(lock); // Temporary to avoid CWG 1579 + return result; } bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override {