Skip to content

Commit f2c416b

Browse files
committed
Merge #16995: Fix gcc 9 warnings
ff9c671 refactor: Work around GCC 9 `-Wredundant-move` warning (Russell Yanofsky) b837b33 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 ff9c671 -- patch looks correct sipa: utACK ff9c671 ryanofsky: Code review ACK ff9c671. Looks good and seems to pass travis, modulo a timeout on one build hebasto: ACK ff9c671, tested on Fedora 31: Tree-SHA512: 52d8c13aaf0d56f9bc546a98d7f853eae21f7e325b202fdeb2286b19a9a0ee308634c644b039f60ad8043421e382381cbf1bce58d9f807547f928621c7d245d0
2 parents 210b533 + ff9c671 commit f2c416b

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/interfaces/chain.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,10 @@ class ChainImpl : public Chain
234234
explicit ChainImpl(NodeContext& node) : m_node(node) {}
235235
std::unique_ptr<Chain::Lock> lock(bool try_lock) override
236236
{
237-
auto result = MakeUnique<LockImpl>(::cs_main, "cs_main", __FILE__, __LINE__, try_lock);
238-
if (try_lock && result && !*result) return {};
239-
// std::move necessary on some compilers due to conversion from
240-
// LockImpl to Lock pointer
241-
return std::move(result);
237+
auto lock = MakeUnique<LockImpl>(::cs_main, "cs_main", __FILE__, __LINE__, try_lock);
238+
if (try_lock && lock && !*lock) return {};
239+
std::unique_ptr<Chain::Lock> result = std::move(lock); // Temporary to avoid CWG 1579
240+
return result;
242241
}
243242
bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override
244243
{

src/protocol.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
8585
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
8686
{
8787
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
88-
memset(pchCommand, 0, sizeof(pchCommand));
89-
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
88+
89+
// Copy the command name, zero-padding to COMMAND_SIZE bytes
90+
size_t i = 0;
91+
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
92+
assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
93+
for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;
94+
9095
nMessageSize = nMessageSizeIn;
9196
memset(pchChecksum, 0, CHECKSUM_SIZE);
9297
}

src/protocol.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class CMessageHeader
3737
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
3838

3939
explicit CMessageHeader(const MessageStartChars& pchMessageStartIn);
40+
41+
/** Construct a P2P message header from message-start characters, a command and the size of the message.
42+
* @note Passing in a `pszCommand` longer than COMMAND_SIZE will result in a run-time assertion error.
43+
*/
4044
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
4145

4246
std::string GetCommand() const;

0 commit comments

Comments
 (0)