Avoid strncpy for CMessageHeader::pchCommand #18367

pull sipa wants to merge 1 commits into bitcoin:master from sipa:202003_strncopy_warning changing 1 files +6 −2
  1. sipa commented at 7:27 PM on March 17, 2020: member

    In GCC 9.2.1, I get the warning:

    In file included from /usr/include/string.h:494,
                     from /usr/include/c++/9/cstring:42,
                     from ./serialize.h:12,
                     from ./netaddress.h:13,
                     from ./protocol.h:13,
                     from protocol.cpp:6:
    In function ‘char* strncpy(char*, const char*, size_t)’,
        inlined from ‘CMessageHeader::CMessageHeader(const unsigned char (&)[4], const char*, unsigned int)’ at protocol.cpp:89:12:
    /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:34: warning: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ specified bound 12 equals destination size [-Wstringop-truncation]
      106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
          |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    The warning is spurious, as we exactly want the truncation behaviour here (no zero padding in pchCommand when the input has length exactly 12), but it seems hard to avoid. The reason is that strncopy is designed to operate on C strings, and our target isn't exactly a C string.

    I'm changing it here to a simple loop that assigns to every byte in the output once, but if desirable, a solution with strlen + memcpy + memset is also possible.

  2. Avoid strncopy for CMessageHeader::pchCommand 69bc566316
  3. sipa renamed this:
    Avoid strncopy for CMessageHeader::pchCommand
    Avoid strncpy for CMessageHeader::pchCommand
    on Mar 17, 2020
  4. DrahtBot added the label P2P on Mar 17, 2020
  5. practicalswift commented at 10:38 PM on March 17, 2020: contributor

    Concept ACK

    FWIW -- C string function usage:

    $ man 3 string | tr " *()<>.," "\n" | grep ^str | grep -v ^string | \
          sort -u | tr "\n" "|" | sed 's/|$//g'; echo
    strcasecmp|strcat|strchr|strcmp|strcoll|strcpy|strcspn|strdup|strfry|strlen|strncasecmp|strncat|strncmp|strncpy|strpbrk|strrchr|strsep|strspn|strstr|strtok|strxfrm
    $ git grep -E $(man 3 string | tr " *()<>.," "\n" | grep ^str | \
          grep -v ^string | sort -u | tr "\n" "|" | sed 's/|$//g') -- "*.cpp" "*.h" \
          ":(exclude)src/leveldb/" ":(exclude)src/secp256k1/" ":(exclude)src/univalue/"
    src/base58.cpp:    int size = strlen(psz) * 733 / 1000 + 1; // log(58) / log(256), rounded up.
    src/chainparams.cpp:    txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
    src/core_read.cpp:            if (strcmp(name, "OP_UNKNOWN") == 0)
    src/net.cpp:            if (strcmp(ifa->ifa_name, "lo") == 0) continue;
    src/net.cpp:            if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
    src/protocol.cpp:    strncpy(pchCommand, pszCommand, COMMAND_SIZE);
    src/randomenv.cpp:        hasher.Write((const unsigned char*)path, strlen(path) + 1);
    src/randomenv.cpp:    hasher.Write((const unsigned char*)COMPILER_VERSION, strlen(COMPILER_VERSION) + 1);
    src/randomenv.cpp:    if (platform_str) hasher.Write((const unsigned char*)platform_str, strlen(platform_str) + 1);
    src/randomenv.cpp:    if (exec_str) hasher.Write((const unsigned char*)exec_str, strlen(exec_str) + 1);
    src/randomenv.cpp:        hasher.Write((const unsigned char*)ifit->ifa_name, strlen(ifit->ifa_name) + 1);
    src/randomenv.cpp:        hasher.Write((const unsigned char*)&name.sysname, strlen(name.sysname) + 1);
    src/randomenv.cpp:        hasher.Write((const unsigned char*)&name.nodename, strlen(name.nodename) + 1);
    src/randomenv.cpp:        hasher.Write((const unsigned char*)&name.release, strlen(name.release) + 1);
    src/randomenv.cpp:        hasher.Write((const unsigned char*)&name.version, strlen(name.version) + 1);
    src/randomenv.cpp:        hasher.Write((const unsigned char*)&name.machine, strlen(name.machine) + 1);
    src/randomenv.cpp:            hasher.Write((const unsigned char*)environ[i], strlen(environ[i]));
    src/rest.cpp:        if (strlen(rf_names[i].name) > 0) {
    src/test/serialize_tests.cpp:                strcmp(charstrval, rhs.charstrval) == 0 && \
    src/test/serialize_tests.cpp:    return strcmp(expectedException.what(), ex.what()) == 0;
    src/test/streams_tests.cpp:        BOOST_CHECK(strstr(e.what(),
    src/test/streams_tests.cpp:        BOOST_CHECK(strstr(e.what(),
    src/test/streams_tests.cpp:        BOOST_CHECK(strstr(e.what(),
    src/util/strencodings.cpp:    val.reserve(strlen(p));
    src/util/strencodings.cpp:    val.reserve(strlen(p));
    src/util/string.h:    return str.size() == strlen(str.c_str());
    src/util/system.cpp:    if (pszHome == nullptr || strlen(pszHome) == 0)
    src/wallet/db.cpp:    fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
    src/wallet/db.cpp:    bool fCreate = strchr(pszMode, 'c') != nullptr;
    src/wallet/db.cpp:                                strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
    src/wallet/db.cpp:                            if (strncmp(ssKey.data(), "\x07version", 8) == 0) {
    src/zmq/zmqpublishnotifier.cpp:    int rc = zmq_send_multipart(psocket, command, strlen(command), data, size, msgseq, (size_t)sizeof(uint32_t), nullptr);
    
  6. DrahtBot commented at 12:11 AM on March 18, 2020: member

    <!--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:

    • #16995 (Fix gcc 9 warnings by laanwj)

    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.

  7. sipa commented at 12:21 AM on March 18, 2020: member

    Closing in favor of #16995.

  8. sipa closed this on Mar 18, 2020

  9. DrahtBot locked this on Feb 15, 2022
Labels

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-19 09:14 UTC

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