Skip to content

Commit b04d51a

Browse files
VoltrexKeyvatargos
authored andcommitted
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: #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>
1 parent 456fca0 commit b04d51a

8 files changed

+15
-14
lines changed

src/cares_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
17501750
}
17511751

17521752
if (err == 0)
1753-
err = ares_set_servers_ports(channel->cares_channel(), &servers[0]);
1753+
err = ares_set_servers_ports(channel->cares_channel(), servers.data());
17541754
else
17551755
err = ARES_EBADSTR;
17561756

src/inspector_socket.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ static void generate_accept_string(const std::string& client_key,
147147
static const char ws_magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
148148
std::string input(client_key + ws_magic);
149149
char hash[SHA_DIGEST_LENGTH];
150-
USE(SHA1(reinterpret_cast<const unsigned char*>(&input[0]), input.size(),
151-
reinterpret_cast<unsigned char*>(hash)));
150+
USE(SHA1(reinterpret_cast<const unsigned char*>(input.data()),
151+
input.size(),
152+
reinterpret_cast<unsigned char*>(hash)));
152153
node::base64_encode(hash, sizeof(hash), *buffer, sizeof(*buffer));
153154
}
154155

src/inspector_socket_server.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void SendProtocolJson(InspectorSocket* socket) {
136136
strm.next_in = const_cast<uint8_t*>(PROTOCOL_JSON + 3);
137137
strm.avail_in = sizeof(PROTOCOL_JSON) - 3;
138138
std::string data(kDecompressedSize, '\0');
139-
strm.next_out = reinterpret_cast<Byte*>(&data[0]);
139+
strm.next_out = reinterpret_cast<Byte*>(data.data());
140140
strm.avail_out = data.size();
141141
CHECK_EQ(Z_STREAM_END, inflate(&strm, Z_FINISH));
142142
CHECK_EQ(0, strm.avail_out);

src/node.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,9 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
733733
std::vector<char*> v8_args_as_char_ptr(v8_args.size());
734734
if (v8_args.size() > 0) {
735735
for (size_t i = 0; i < v8_args.size(); ++i)
736-
v8_args_as_char_ptr[i] = &v8_args[i][0];
736+
v8_args_as_char_ptr[i] = v8_args[i].data();
737737
int argc = v8_args.size();
738-
V8::SetFlagsFromCommandLine(&argc, &v8_args_as_char_ptr[0], true);
738+
V8::SetFlagsFromCommandLine(&argc, v8_args_as_char_ptr.data(), true);
739739
v8_args_as_char_ptr.resize(argc);
740740
}
741741

src/node_file.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
10741074
} while (static_cast<size_t>(numchars) == kBlockSize);
10751075

10761076
size_t start = 0;
1077-
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
1077+
if (offset >= 3 && 0 == memcmp(chars.data(), "\xEF\xBB\xBF", 3)) {
10781078
start = 3; // Skip UTF-8 BOM.
10791079
}
10801080

src/string_bytes.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ size_t StringBytes::hex_encode(
541541
std::string StringBytes::hex_encode(const char* src, size_t slen) {
542542
size_t dlen = slen * 2;
543543
std::string dst(dlen, '\0');
544-
hex_encode(src, slen, &dst[0], dlen);
544+
hex_encode(src, slen, dst.data(), dlen);
545545
return dst;
546546
}
547547

src/util.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ std::string GetProcessTitle(const char* default_title) {
144144
std::string buf(16, '\0');
145145

146146
for (;;) {
147-
const int rc = uv_get_process_title(&buf[0], buf.size());
147+
const int rc = uv_get_process_title(buf.data(), buf.size());
148148

149149
if (rc == 0)
150150
break;
@@ -160,7 +160,7 @@ std::string GetProcessTitle(const char* default_title) {
160160

161161
// Strip excess trailing nul bytes. Using strlen() here is safe,
162162
// uv_get_process_title() always zero-terminates the result.
163-
buf.resize(strlen(&buf[0]));
163+
buf.resize(strlen(buf.data()));
164164

165165
return buf;
166166
}

test/cctest/test_inspector_socket.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,8 @@ TEST_F(InspectorSocketTest, Send1Mb) {
764764
std::string expected(EXPECTED_FRAME_HEADER, sizeof(EXPECTED_FRAME_HEADER));
765765
expected.append(message);
766766

767-
delegate->Write(&message[0], message.size());
768-
expect_on_client(&expected[0], expected.size());
767+
delegate->Write(message.data(), message.size());
768+
expect_on_client(expected.data(), expected.size());
769769

770770
char MASK[4] = {'W', 'h', 'O', 'a'};
771771

@@ -778,8 +778,8 @@ TEST_F(InspectorSocketTest, Send1Mb) {
778778
outgoing.resize(outgoing.size() + message.size());
779779
mask_message(message, &outgoing[sizeof(FRAME_TO_SERVER_HEADER)], MASK);
780780

781-
do_write(&outgoing[0], outgoing.size());
782-
delegate->ExpectData(&message[0], message.size());
781+
do_write(outgoing.data(), outgoing.size());
782+
delegate->ExpectData(message.data(), message.size());
783783

784784
// 3. Close
785785
const char CLIENT_CLOSE_FRAME[] = {'\x88', '\x80', '\x2D',

0 commit comments

Comments
 (0)