Skip to content

Commit

Permalink
src: prefer data accessor of string and vector
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
VoltrexKeyva authored and targos committed May 12, 2023
1 parent 456fca0 commit b04d51a
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions src/inspector_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/inspector_socket_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
2 changes: 1 addition & 1 deletion src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions test/cctest/test_inspector_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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'};

Expand All @@ -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',
Expand Down

0 comments on commit b04d51a

Please sign in to comment.