-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: use std:: prefix for std lib funcs #25172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
I believe some were missed: Git diffdiff --git a/src/addrdb.cpp b/src/addrdb.cpp
index 31f8eadf9..cd124d157 100644
--- a/src/addrdb.cpp
+++ b/src/addrdb.cpp
@@ -92,7 +92,7 @@ void DeserializeDB(Stream& stream, Data& data, bool fCheckSum = true)
unsigned char pchMsgTmp[4];
verifier >> pchMsgTmp;
// ... verify the network matches ours
- if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) {
+ if (std::memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) {
throw std::runtime_error{"Invalid network magic number"};
}
diff --git a/src/base58.cpp b/src/base58.cpp
index 11c1ce739..de9a82d46 100644
--- a/src/base58.cpp
+++ b/src/base58.cpp
@@ -150,7 +150,7 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
}
// re-calculate the checksum, ensure it matches the included 4-byte checksum
uint256 hash = Hash(Span{vchRet}.first(vchRet.size() - 4));
- if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
+ if (std::memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
vchRet.clear();
return false;
}
diff --git a/src/net.cpp b/src/net.cpp
index 88f4cef5b..47ccede26 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -732,7 +732,7 @@ int V1TransportDeserializer::readHeader(Span<const uint8_t> msg_bytes)
}
// Check start string, network magic
- if (memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
+ if (std::memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
LogPrint(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
return -1;
}
@@ -793,7 +793,7 @@ CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds
RandAddEvent(ReadLE32(hash.begin()));
// Check checksum and header message type string
- if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
+ if (std::memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
LogPrint(BCLog::NET, "Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n",
SanitizeString(msg.m_type), msg.m_message_size,
HexStr(Span{hash}.first(CMessageHeader::CHECKSUM_SIZE)),
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index 17ab226a3..88a90e256 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -768,7 +768,7 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
filein >> blk_start >> blk_size;
- if (memcmp(blk_start, message_start, CMessageHeader::MESSAGE_START_SIZE)) {
+ if (std::memcmp(blk_start, message_start, CMessageHeader::MESSAGE_START_SIZE)) {
return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
HexStr(blk_start),
HexStr(message_start));
diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp
index c4d13d728..1cabf65ac 100644
--- a/src/script/interpreter.cpp
+++ b/src/script/interpreter.cpp
@@ -1877,7 +1877,7 @@ static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion,
exec_script = CScript(script_bytes.begin(), script_bytes.end());
uint256 hash_exec_script;
CSHA256().Write(exec_script.data(), exec_script.size()).Finalize(hash_exec_script.begin());
- if (memcmp(hash_exec_script.begin(), program.data(), 32)) {
+ if (std::memcmp(hash_exec_script.begin(), program.data(), 32)) {
return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH);
}
return ExecuteWitnessScript(stack, exec_script, flags, SigVersion::WITNESS_V0, checker, execdata, serror);
diff --git a/src/validation.cpp b/src/validation.cpp
index fefbe033e..b3fcba2cd 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -3548,7 +3548,7 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-nonce-size", strprintf("%s : invalid witness reserved value size", __func__));
}
CHash256().Write(hashWitness).Write(block.vtx[0]->vin[0].scriptWitness.stack[0]).Finalize(hashWitness);
- if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
+ if (std::memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-merkle-match", strprintf("%s : witness merkle commitment mismatch", __func__));
}
fHaveWitness = true;
@@ -4294,7 +4294,7 @@ void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
blkdat.FindByte(m_params.MessageStart()[0]);
nRewind = blkdat.GetPos() + 1;
blkdat >> buf;
- if (memcmp(buf, m_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
+ if (std::memcmp(buf, m_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
continue;
}
// read size |
1b7b070
to
1d2d121
Compare
Thanks. Have addressed more cases. |
14d5f20
to
7749a28
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 7749a28
No problem with this, I agree it's good to be consistent here, though not generally a fan of 'all-over-the-place' PRs. I do think we need a way to enforce this to prevent reintroducing unqualified std functions, for example adapt one of the linters. |
In the spirit of "Include what you use", we should also ensure we are including the appropriate headers for these functions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strong ACK 7749a28
-BEGIN VERIFY SCRIPT- sed -i -e "s/ memset(/ std::memset(/" $(git grep -l "memset(" -- ":(exclude)src/bench/nanobench.h" ":(exclude)src/secp256k1" ":(exclude)src/crc32c" ":(exclude)src/leveldb" ":(exclude)test/functional") sed -i -e "s/string.h/cstring/" src/crypto/aes.cpp src/crypto/chacha20.cpp src/crypto/chacha_poly_aead.cpp src/crypto/hmac_sha256.cpp src/crypto/hmac_sha512.cpp -END VERIFY SCRIPT-
-BEGIN VERIFY SCRIPT- sed -i -e "s/ memcmp(/ std::memcmp(/" $(git grep -l "memcmp(" -- ":(exclude)src/secp256k1" ":(exclude)src/leveldb" src) sed -i -e "s/(memcmp(/(std::memcmp(/" $(git grep -l "memcmp(" -- ":(exclude)src/secp256k1" ":(exclude)src/leveldb" ":(exclude)src/crypto/ctaes" src) -END VERIFY SCRIPT-
-BEGIN VERIFY SCRIPT- sed -i -e "s/ memcpy(/ std::memcpy(/" $(git grep -l "memcpy(" -- ":(exclude)src/secp256k1" ":(exclude)src/leveldb" src) sed -i -e "s/string.h/cstring/" src/crypto/common.h -END VERIFY SCRIPT-
7749a28
to
8dd3c4a
Compare
Concept ACK. |
Was #25172 (comment) addressed? |
Not yet, but I am going to address this via clang-tidy plugin, rather than a python linter. Although there a few steps before that will be available. |
Looks like Clang is moving in the direction of adding warnings for unqualified std function usage, beginning with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 8dd3c4a, I have reviewed the code and it looks OK, I agree it can be merged.
Well, if we want the changes, I'd still prefer to enforce them. If we don't, then we could close this pull. |
🐙 This pull request conflicts with the target branch and needs rebase. Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a "draft". |
We currently use a mix of
std::mem*(
andmem*(
throughout the codebase. Consolidate tostd::mem*(
.Follows up to a comment in #24962:
Also includes a change to
std::vsnprintf
and header changes required to perform compilation.