Skip to content

Commit 5728f88

Browse files
committed
Merge bitcoin#17280: refactor: Change occurences of c_str() used with 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
2 parents ecad0a8 + f3b51eb commit 5728f88

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
366366
std::string endpoint = "/";
367367
if (!gArgs.GetArgs("-rpcwallet").empty()) {
368368
std::string walletName = gArgs.GetArg("-rpcwallet", "");
369-
char *encodedURI = evhttp_uriencode(walletName.c_str(), walletName.size(), false);
369+
char *encodedURI = evhttp_uriencode(walletName.data(), walletName.size(), false);
370370
if (encodedURI) {
371371
endpoint = "/wallet/"+ std::string(encodedURI);
372372
free(encodedURI);

src/crypto/hkdf_sha256_32.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
CHKDF_HMAC_SHA256_L32::CHKDF_HMAC_SHA256_L32(const unsigned char* ikm, size_t ikmlen, const std::string& salt)
1111
{
12-
CHMAC_SHA256((const unsigned char*)salt.c_str(), salt.size()).Write(ikm, ikmlen).Finalize(m_prk);
12+
CHMAC_SHA256((const unsigned char*)salt.data(), salt.size()).Write(ikm, ikmlen).Finalize(m_prk);
1313
}
1414

1515
void CHKDF_HMAC_SHA256_L32::Expand32(const std::string& info, unsigned char hash[OUTPUT_SIZE])

src/fs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ std::string get_filesystem_error_message(const fs::filesystem_error& e)
107107
#else
108108
// Convert from Multi Byte to utf-16
109109
std::string mb_string(e.what());
110-
int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), nullptr, 0);
110+
int size = MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), nullptr, 0);
111111

112112
std::wstring utf16_string(size, L'\0');
113-
MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
113+
MultiByteToWideChar(CP_ACP, 0, mb_string.data(), mb_string.size(), &*utf16_string.begin(), size);
114114
// Convert from utf-16 to utf-8
115115
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
116116
#endif

src/httprpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static bool multiUserAuthorized(std::string strUserPass)
112112
static const unsigned int KEY_SIZE = 32;
113113
unsigned char out[KEY_SIZE];
114114

115-
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);
115+
CHMAC_SHA256(reinterpret_cast<const unsigned char*>(strSalt.data()), strSalt.size()).Write(reinterpret_cast<const unsigned char*>(strPass.data()), strPass.size()).Finalize(out);
116116
std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
117117
std::string strHashFromPass = HexStr(hexvec);
118118

src/util/strencodings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ std::string EncodeBase64(const unsigned char* pch, size_t len)
138138

139139
std::string EncodeBase64(const std::string& str)
140140
{
141-
return EncodeBase64((const unsigned char*)str.c_str(), str.size());
141+
return EncodeBase64((const unsigned char*)str.data(), str.size());
142142
}
143143

144144
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
@@ -207,7 +207,7 @@ std::string EncodeBase32(const unsigned char* pch, size_t len)
207207

208208
std::string EncodeBase32(const std::string& str)
209209
{
210-
return EncodeBase32((const unsigned char*)str.c_str(), str.size());
210+
return EncodeBase32((const unsigned char*)str.data(), str.size());
211211
}
212212

213213
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)

src/wallet/crypter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, cons
2323
unsigned char buf[CSHA512::OUTPUT_SIZE];
2424
CSHA512 di;
2525

26-
di.Write((const unsigned char*)strKeyData.c_str(), strKeyData.size());
26+
di.Write((const unsigned char*)strKeyData.data(), strKeyData.size());
2727
di.Write(chSalt.data(), chSalt.size());
2828
di.Finalize(buf);
2929

0 commit comments

Comments
 (0)