refactor: Change occurences of c_str() used with size() to data() #17280

pull laanwj wants to merge 1 commits into bitcoin:master from laanwj:2019_10_c_str_size changing 6 files +8 −8
  1. laanwj commented at 12:51 PM on October 28, 2019: member

    Using data() better communicates the intent here.

    Also, depending on how c_str() is implemented, this fixes undefined behavior: The part of the string after the first NULL character might have undefined contents (or even be inaccessible, worst case). Apparently this is no longer an issue with C++11.

  2. Fix occurences of c_str() used with size() to data()
    Using `data()` better communicates the intent here.
    
    Also, depending on how `c_str()` is implemented, this fixes undefined
    behavior: The part of the string after the first NULL character might
    have undefined contents.
    f3b51eb935
  3. laanwj added the label Bug on Oct 28, 2019
  4. fjahr commented at 2:25 PM on October 28, 2019: member

    Code review ACK f3b51eb

  5. practicalswift commented at 2:53 PM on October 28, 2019: contributor

    Concept ACK

    What about linting this to make sure this is a one-time fix-up?

    Suggested lint command:

    $ git grep -E '([a-zA-Z0-9_]+).c_str\(\)+[^a-zA-Z0-9_]+\1\.size\(\)' -- "*.cpp" "*.h"
    src/bitcoin-cli.cpp:        char *encodedURI = evhttp_uriencode(walletName.c_str(), walletName.size(), false);
    src/crypto/hkdf_sha256_32.cpp:    CHMAC_SHA256((const unsigned char*)salt.c_str(), salt.size()).Write(ikm, ikmlen).Finalize(m_prk);
    src/fs.cpp:    int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), nullptr, 0);
    src/fs.cpp:    MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
    src/httprpc.cpp:        CHMAC_SHA256(reinterpret_cast<const unsigned char*>(strSalt.c_str()), strSalt.size()).Write(reinterpret_cast<const unsigned char*>(strPass.c_str()), strPass.size()).Finalize(out);
    src/util/strencodings.cpp:    return EncodeBase64((const unsigned char*)str.c_str(), str.size());
    src/util/strencodings.cpp:    return EncodeBase32((const unsigned char*)str.c_str(), str.size());
    src/wallet/crypter.cpp:    di.Write((const unsigned char*)strKeyData.c_str(), strKeyData.size());
    
  6. laanwj commented at 3:19 PM on October 28, 2019: member

    What about linting this to make sure this is a one-time fix-up?

    Maybe—It could catch some of them, I guess. But only if they're on the same line? (which is the case for all of these, admittedly)

  7. practicalswift commented at 3:34 PM on October 28, 2019: contributor

    @laanwj Correct, multi-line search with git grep is not possible AFAIK, so there could be false negatives.

  8. ryanofsky approved
  9. ryanofsky commented at 4:17 PM on October 28, 2019: member

    Code review ACK f3b51eb9352d7a7c5dfa15615efc8bc0a52ffecf. Most of these calls (including one in crypter.cpp) are passing text strings, not binary strings likely to contain \0 and were probably safe before, but much better to avoid the possibility of bugs like this.

  10. MarcoFalke removed the label Bug on Oct 28, 2019
  11. MarcoFalke added the label Refactoring on Oct 28, 2019
  12. MarcoFalke commented at 7:37 PM on October 28, 2019: member

    We are using C++11 and according to https://en.cppreference.com/w/cpp/string/basic_string/c_str this is safe.

    c_str() and data() perform the same function. | (since C++11)

    I have removed the "bug" label and added the "Refactoring" label.

    Concept ACK, though.

  13. practicalswift commented at 11:20 AM on October 29, 2019: contributor

    ACK f3b51eb9352d7a7c5dfa15615efc8bc0a52ffecf -- diff looks correct, data() more idiomatic

  14. laanwj renamed this:
    Fix occurences of c_str() used with size() to data()
    refactor: Change occurences of c_str() used with size() to data()
    on Oct 30, 2019
  15. laanwj referenced this in commit 5728f88d64 on Oct 30, 2019
  16. laanwj merged this on Oct 30, 2019
  17. laanwj closed this on Oct 30, 2019

  18. sidhujag referenced this in commit d7a4574b8a on Oct 30, 2019
  19. jasonbcox referenced this in commit 81d108f6f2 on Sep 9, 2020
  20. sidhujag referenced this in commit 401599fe37 on Nov 10, 2020
  21. kittywhiskers referenced this in commit 2c13748489 on May 19, 2021
  22. kittywhiskers referenced this in commit d9489c72af on May 19, 2021
  23. kittywhiskers referenced this in commit bc77b4349a on May 20, 2021
  24. kittywhiskers referenced this in commit 61bfe7b8ab on May 20, 2021
  25. kittywhiskers referenced this in commit 6590f56c43 on May 20, 2021
  26. kittywhiskers referenced this in commit 6a73b70131 on May 20, 2021
  27. PastaPastaPasta referenced this in commit b76e7fec1f on May 21, 2021
  28. MarcoFalke locked this on Dec 16, 2021

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-13 15:14 UTC

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