Skip to content

Commit 559c98f

Browse files
VoltrexKeyvaMoLow
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 e4539e1 commit 559c98f

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
@@ -754,9 +754,9 @@ int ProcessGlobalArgs(std::vector<std::string>* args,
754754
std::vector<char*> v8_args_as_char_ptr(v8_args.size());
755755
if (v8_args.size() > 0) {
756756
for (size_t i = 0; i < v8_args.size(); ++i)
757-
v8_args_as_char_ptr[i] = &v8_args[i][0];
757+
v8_args_as_char_ptr[i] = v8_args[i].data();
758758
int argc = v8_args.size();
759-
V8::SetFlagsFromCommandLine(&argc, &v8_args_as_char_ptr[0], true);
759+
V8::SetFlagsFromCommandLine(&argc, v8_args_as_char_ptr.data(), true);
760760
v8_args_as_char_ptr.resize(argc);
761761
}
762762

src/node_file.cc

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

10521052
size_t start = 0;
1053-
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
1053+
if (offset >= 3 && 0 == memcmp(chars.data(), "\xEF\xBB\xBF", 3)) {
10541054
start = 3; // Skip UTF-8 BOM.
10551055
}
10561056

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
@@ -140,7 +140,7 @@ std::string GetProcessTitle(const char* default_title) {
140140
std::string buf(16, '\0');
141141

142142
for (;;) {
143-
const int rc = uv_get_process_title(&buf[0], buf.size());
143+
const int rc = uv_get_process_title(buf.data(), buf.size());
144144

145145
if (rc == 0)
146146
break;
@@ -156,7 +156,7 @@ std::string GetProcessTitle(const char* default_title) {
156156

157157
// Strip excess trailing nul bytes. Using strlen() here is safe,
158158
// uv_get_process_title() always zero-terminates the result.
159-
buf.resize(strlen(&buf[0]));
159+
buf.resize(strlen(buf.data()));
160160

161161
return buf;
162162
}

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)