src: prefer data accessor of string and vector

The pattern of getting the address of the element at index 0 of a
container is generally used to materialize a pointer to the backing
data of a container, however `std::string` and `std::vector`
provide a `data()` accessor to retrieve the data pointer which
should be preferred.

This also ensures that in the case that the container is empty, the
data pointer access does not perform an errant memory access.

PR-URL: https://github.com/nodejs/node/pull/47750
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Mohammed Keyvanzadeh 2023-05-03 17:39:23 +03:30 committed by GitHub
parent 5466bec2c5
commit dd6eb67b1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 14 deletions

View File

@ -1750,7 +1750,7 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
}
if (err == 0)
err = ares_set_servers_ports(channel->cares_channel(), &servers[0]);
err = ares_set_servers_ports(channel->cares_channel(), servers.data());
else
err = ARES_EBADSTR;

View File

@ -147,8 +147,9 @@ static void generate_accept_string(const std::string& client_key,
static const char ws_magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
std::string input(client_key + ws_magic);
char hash[SHA_DIGEST_LENGTH];
USE(SHA1(reinterpret_cast<const unsigned char*>(&input[0]), input.size(),
reinterpret_cast<unsigned char*>(hash)));
USE(SHA1(reinterpret_cast<const unsigned char*>(input.data()),
input.size(),
reinterpret_cast<unsigned char*>(hash)));
node::base64_encode(hash, sizeof(hash), *buffer, sizeof(*buffer));
}

View File

@ -136,7 +136,7 @@ void SendProtocolJson(InspectorSocket* socket) {
strm.next_in = const_cast<uint8_t*>(PROTOCOL_JSON + 3);
strm.avail_in = sizeof(PROTOCOL_JSON) - 3;
std::string data(kDecompressedSize, '\0');
strm.next_out = reinterpret_cast<Byte*>(&data[0]);
strm.next_out = reinterpret_cast<Byte*>(data.data());
strm.avail_out = data.size();
CHECK_EQ(Z_STREAM_END, inflate(&strm, Z_FINISH));
CHECK_EQ(0, strm.avail_out);

View File

@ -733,9 +733,9 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
std::vector<char*> v8_args_as_char_ptr(v8_args.size());
if (v8_args.size() > 0) {
for (size_t i = 0; i < v8_args.size(); ++i)
v8_args_as_char_ptr[i] = &v8_args[i][0];
v8_args_as_char_ptr[i] = v8_args[i].data();
int argc = v8_args.size();
V8::SetFlagsFromCommandLine(&argc, &v8_args_as_char_ptr[0], true);
V8::SetFlagsFromCommandLine(&argc, v8_args_as_char_ptr.data(), true);
v8_args_as_char_ptr.resize(argc);
}

View File

@ -1074,7 +1074,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
} while (static_cast<size_t>(numchars) == kBlockSize);
size_t start = 0;
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
if (offset >= 3 && 0 == memcmp(chars.data(), "\xEF\xBB\xBF", 3)) {
start = 3; // Skip UTF-8 BOM.
}

View File

@ -541,7 +541,7 @@ size_t StringBytes::hex_encode(
std::string StringBytes::hex_encode(const char* src, size_t slen) {
size_t dlen = slen * 2;
std::string dst(dlen, '\0');
hex_encode(src, slen, &dst[0], dlen);
hex_encode(src, slen, dst.data(), dlen);
return dst;
}

View File

@ -144,7 +144,7 @@ std::string GetProcessTitle(const char* default_title) {
std::string buf(16, '\0');
for (;;) {
const int rc = uv_get_process_title(&buf[0], buf.size());
const int rc = uv_get_process_title(buf.data(), buf.size());
if (rc == 0)
break;
@ -160,7 +160,7 @@ std::string GetProcessTitle(const char* default_title) {
// Strip excess trailing nul bytes. Using strlen() here is safe,
// uv_get_process_title() always zero-terminates the result.
buf.resize(strlen(&buf[0]));
buf.resize(strlen(buf.data()));
return buf;
}

View File

@ -764,8 +764,8 @@ TEST_F(InspectorSocketTest, Send1Mb) {
std::string expected(EXPECTED_FRAME_HEADER, sizeof(EXPECTED_FRAME_HEADER));
expected.append(message);
delegate->Write(&message[0], message.size());
expect_on_client(&expected[0], expected.size());
delegate->Write(message.data(), message.size());
expect_on_client(expected.data(), expected.size());
char MASK[4] = {'W', 'h', 'O', 'a'};
@ -778,8 +778,8 @@ TEST_F(InspectorSocketTest, Send1Mb) {
outgoing.resize(outgoing.size() + message.size());
mask_message(message, &outgoing[sizeof(FRAME_TO_SERVER_HEADER)], MASK);
do_write(&outgoing[0], outgoing.size());
delegate->ExpectData(&message[0], message.size());
do_write(outgoing.data(), outgoing.size());
delegate->ExpectData(message.data(), message.size());
// 3. Close
const char CLIENT_CLOSE_FRAME[] = {'\x88', '\x80', '\x2D',