refactor: use std:: prefix for std lib funcs #25172

pull fanquake wants to merge 6 commits into bitcoin:master from fanquake:use_std_variants changing 58 files +209 −203
  1. fanquake commented at 3:35 PM on May 19, 2022: member

    We currently use a mix of std::mem*( and mem*( throughout the codebase. Consolidate to std::mem*(.

    Follows up to a comment in #24962:

    Maybe switch to std::memmove, which comes with the documentation:

    Also includes a change to std::vsnprintf and header changes required to perform compilation.

  2. fanquake added the label Refactoring on May 19, 2022
  3. DrahtBot commented at 9:28 AM on May 20, 2022: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #26177 (refactor / kernel: Move non-gArgs chainparams functionality to kernel by TheCharlatan)
    • #26153 (Reduce wasted pseudorandom bytes in ChaCha20 + various improvements by sipa)
    • #25903 (Use static member functions from class instead of instances by aureleoules)
    • #25830 (refactor: Replace m_params with chainman.GetParams() by aureleoules)
    • #25361 (BIP324: Cipher suite by dhruv)
    • #25152 (refactor: Split util/system into exception, shell, and fs-specific files by Empact)
    • #24994 (build: Build libbitcoinconsensus from its own convenience library by hebasto)
    • #23561 (BIP324: Handshake prerequisites by dhruv)

    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.

  4. in src/dbwrapper.cpp:58 in 1b7b070db6 outdated
      41 | @@ -42,7 +42,7 @@ class CBitcoinLevelDBLogger : public leveldb::Logger {
      42 |                      va_list backup_ap;
      43 |                      va_copy(backup_ap, ap);
      44 |                      // Do not use vsnprintf elsewhere in bitcoin source code, see above.
      45 | -                    p += vsnprintf(p, limit - p, format, backup_ap);
      46 | +                    p += std::vsnprintf(p, limit - p, format, backup_ap);
    


    kristapsk commented at 5:13 PM on May 20, 2022:

    This change is with intent or by accident? PR title says "use std:: for mem( funcs", this one isn't mem func. Either title should be changed or this removed from this PR. Also, if this is changed, #include <stdint.h> at the beginning of file should be changed to #include <cstdio>.


    fanquake commented at 6:49 AM on May 23, 2022:

    #include <stdint.h> at the beginning of file should be changed to #include <cstdio>

    stdint.h is still required, and <cstdio> can just be added.

    I don't think this change needs to be dropped, or it's inclusion is confusing. I've added a note to the PR body.


    kristapsk commented at 5:12 PM on May 23, 2022:

    Yes, my mistake, of course <cstdio> does no replace <stdint.h>.

  5. aureleoules commented at 5:17 PM on May 20, 2022: member

    I believe some were missed:

    <details> <summary>Git diff</summary>

    diff --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
    

    </details>

  6. fanquake renamed this:
    refactor: use std:: for mem*( funcs
    refactor: use std:: prefix for std lib funcs
    on May 23, 2022
  7. fanquake force-pushed on May 23, 2022
  8. fanquake commented at 6:59 AM on May 23, 2022: member

    I believe some were missed:

    Thanks. Have addressed more cases.

  9. fanquake force-pushed on May 23, 2022
  10. fanquake force-pushed on May 23, 2022
  11. dunxen approved
  12. dunxen commented at 7:27 PM on May 24, 2022: contributor

    ACK 7749a28

  13. laanwj commented at 1:23 PM on May 26, 2022: member

    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.

  14. Empact commented at 4:35 PM on June 18, 2022: member

    In the spirit of "Include what you use", we should also ensure we are including the appropriate headers for these functions, <cstring> for std::mem* and <cstdlib> for std::getenv.

  15. w0xlt approved
  16. DrahtBot added the label Needs rebase on Jul 19, 2022
  17. refactor: use std::memmove() over memmove() e3f36cdf22
  18. refactor: use std::vsnprintf over vsnprintf 2ce64a910e
  19. scripted-diff: use std::memset() over memset()
    -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-
    8e237c9e85
  20. scripted-diff: use std::memcmp() over memcmp()
    -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-
    7a5b81f433
  21. scripted-diff: use std::memcpy() over memcpy()
    -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-
    5a40961a2e
  22. refactor: use std::getenv() over getenv() 8dd3c4aa74
  23. fanquake force-pushed on Jul 20, 2022
  24. hebasto commented at 4:46 PM on July 20, 2022: member

    Concept ACK.

  25. maflcko commented at 4:54 PM on July 20, 2022: member

    Was #25172 (comment) addressed?

  26. DrahtBot removed the label Needs rebase on Jul 20, 2022
  27. fanquake commented at 4:39 PM on July 22, 2022: member

    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.

  28. maflcko added the label Waiting for author on Jul 23, 2022
  29. fanquake commented at 1:05 PM on July 27, 2022: member

    Looks like Clang is moving in the direction of adding warnings for unqualified std function usage, beginning with std::move and std::forward. Starting with LLVM 15 there will be an -Wunqualified-std-cast-call warning. See https://reviews.llvm.org/D119670.

  30. hebasto approved
  31. hebasto commented at 1:09 PM on July 27, 2022: member

    ACK 8dd3c4aa74c064ae8a926220480ce55eba2517f3, I have reviewed the code and it looks OK, I agree it can be merged.

  32. glozow removed the label Waiting for author on Aug 2, 2022
  33. maflcko commented at 8:56 AM on August 3, 2022: member

    glozow removed the Waiting for author label 21 hours ago

    Well, if we want the changes, I'd still prefer to enforce them. If we don't, then we could close this pull.

  34. DrahtBot added the label Needs rebase on Oct 19, 2022
  35. DrahtBot commented at 9:31 AM on October 19, 2022: contributor

    <!--cf906140f33d8803c4a75a2196329ecb-->

    🐙 This pull request conflicts with the target branch and needs rebase.

    <sub>Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a "draft".</sub>

  36. fanquake closed this on Dec 5, 2022

  37. fanquake deleted the branch on Sep 11, 2023
  38. bitcoin locked this on Sep 10, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 06:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me