Skip to content

Conversation

laanwj
Copy link
Member

@laanwj laanwj commented Oct 28, 2019

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.

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.
@laanwj laanwj added the Bug label Oct 28, 2019
@fjahr
Copy link
Contributor

fjahr commented Oct 28, 2019

Code review ACK f3b51eb

@practicalswift
Copy link
Contributor

practicalswift commented Oct 28, 2019

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());

@laanwj
Copy link
Member Author

laanwj commented Oct 28, 2019

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)

@practicalswift
Copy link
Contributor

practicalswift commented Oct 28, 2019

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

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK f3b51eb. 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.

@maflcko maflcko added Refactoring and removed Bug labels Oct 28, 2019
@maflcko
Copy link
Member

maflcko commented Oct 28, 2019

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.

@practicalswift
Copy link
Contributor

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

@laanwj laanwj changed the title Fix occurences of c_str() used with size() to data() refactor: Change occurences of c_str() used with size() to data() Oct 30, 2019
laanwj added a commit that referenced this pull request Oct 30, 2019
… to data()

f3b51eb Fix occurences of c_str() used with size() to data() (Wladimir J. van der Laan)

Pull request description:

  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](#17281 (comment)).

ACKs for top commit:
  fjahr:
    Code review ACK f3b51eb
  practicalswift:
    ACK f3b51eb -- diff looks correct, `data()` more idiomatic
  ryanofsky:
    Code review ACK f3b51eb. 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.

Tree-SHA512: 842e1bdd37efc4ece2ecb87ca34962aafef0a192180051def630607e349dc9c8b4e562481fff3de474515f493b4ee3ea53b00269a801a66e625326a38dfce5b8
@laanwj laanwj merged commit f3b51eb into bitcoin:master Oct 30, 2019
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 30, 2019
… size() to data()

f3b51eb Fix occurences of c_str() used with size() to data() (Wladimir J. van der Laan)

Pull request description:

  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](bitcoin#17281 (comment)).

ACKs for top commit:
  fjahr:
    Code review ACK f3b51eb
  practicalswift:
    ACK f3b51eb -- diff looks correct, `data()` more idiomatic
  ryanofsky:
    Code review ACK f3b51eb. 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.

Tree-SHA512: 842e1bdd37efc4ece2ecb87ca34962aafef0a192180051def630607e349dc9c8b4e562481fff3de474515f493b4ee3ea53b00269a801a66e625326a38dfce5b8
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 9, 2020
…e() to data()

Summary:
Fix occurences of c_str() used with size() to data() (Wladimir J. van der Laan)

Pull request description:

  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](bitcoin/bitcoin#17281 (comment)).

---

Backport of Core [[bitcoin/bitcoin#17280 | PR17280]]

Test Plan:
  ninja check check-functional

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Subscribers: deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D7387
sidhujag pushed a commit to syscoin-core/syscoin that referenced this pull request Nov 10, 2020
… size() to data()

f3b51eb Fix occurences of c_str() used with size() to data() (Wladimir J. van der Laan)

Pull request description:

  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](bitcoin#17281 (comment)).

ACKs for top commit:
  fjahr:
    Code review ACK f3b51eb
  practicalswift:
    ACK f3b51eb -- diff looks correct, `data()` more idiomatic
  ryanofsky:
    Code review ACK f3b51eb. 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.

Tree-SHA512: 842e1bdd37efc4ece2ecb87ca34962aafef0a192180051def630607e349dc9c8b4e562481fff3de474515f493b4ee3ea53b00269a801a66e625326a38dfce5b8
kwvg pushed a commit to kwvg/dash that referenced this pull request May 19, 2021
kwvg added a commit to kwvg/dash that referenced this pull request May 19, 2021
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants