Merge #16995: Fix gcc 9 warnings

ff9c671b11d40e5d0623eff3dd12e48cbaafb34e refactor: Work around GCC 9 `-Wredundant-move` warning (Russell Yanofsky)
b837b334db5dd6232725fd2350928ff4fbd3feee net: Fail instead of truncate command name in CMessageHeader (Wladimir J. van der Laan)

Pull request description:

  Fixes all 3 from #16992 (see commits)

  - net: Fail instead of truncate command name in CMessageHeader
  - refactor: Use std::move workaround for unique_ptr upcast only when necessary

ACKs for top commit:
  practicalswift:
    ACK ff9c671b11d40e5d0623eff3dd12e48cbaafb34e -- patch looks correct
  sipa:
    utACK ff9c671b11d40e5d0623eff3dd12e48cbaafb34e
  ryanofsky:
    Code review ACK ff9c671b11d40e5d0623eff3dd12e48cbaafb34e. Looks good and seems to pass travis, modulo a timeout on one build
  hebasto:
    ACK ff9c671b11d40e5d0623eff3dd12e48cbaafb34e, tested on Fedora 31:

Tree-SHA512: 52d8c13aaf0d56f9bc546a98d7f853eae21f7e325b202fdeb2286b19a9a0ee308634c644b039f60ad8043421e382381cbf1bce58d9f807547f928621c7d245d0
This commit is contained in:
Wladimir J. van der Laan 2020-03-27 19:54:49 +01:00 committed by Pasta
parent e99f1ee057
commit 1e30366c40
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
3 changed files with 15 additions and 7 deletions

View File

@ -268,11 +268,10 @@ class ChainImpl : public Chain
public: public:
std::unique_ptr<Chain::Lock> lock(bool try_lock) override std::unique_ptr<Chain::Lock> lock(bool try_lock) override
{ {
auto result = MakeUnique<LockImpl>(::cs_main, "cs_main", __FILE__, __LINE__, try_lock); auto lock = MakeUnique<LockImpl>(::cs_main, "cs_main", __FILE__, __LINE__, try_lock);
if (try_lock && result && !*result) return {}; if (try_lock && lock && !*lock) return {};
// std::move necessary on some compilers due to conversion from std::unique_ptr<Chain::Lock> result = std::move(lock); // Temporary to avoid CWG 1579
// LockImpl to Lock pointer return result;
return std::move(result);
} }
bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override
{ {

View File

@ -178,8 +178,13 @@ CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn) CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
{ {
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE); 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; nMessageSize = nMessageSizeIn;
memset(pchChecksum, 0, CHECKSUM_SIZE); memset(pchChecksum, 0, CHECKSUM_SIZE);
} }

View File

@ -37,6 +37,10 @@ public:
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE]; typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
explicit CMessageHeader(const MessageStartChars& pchMessageStartIn); 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); CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
std::string GetCommand() const; std::string GetCommand() const;