From b04d51a0b512e3a5d152e680635f7e033cad35c3 Mon Sep 17 00:00:00 2001 From: Mohammed Keyvanzadeh Date: Wed, 3 May 2023 17:39:23 +0330 Subject: [PATCH] src: prefer data accessor of string and vector MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Daeyeon Jeong Reviewed-By: Chengzhong Wu Reviewed-By: Darshan Sen Reviewed-By: Matteo Collina --- src/cares_wrap.cc | 2 +- src/inspector_socket.cc | 5 +++-- src/inspector_socket_server.cc | 2 +- src/node.cc | 4 ++-- src/node_file.cc | 2 +- src/string_bytes.cc | 2 +- src/util.cc | 4 ++-- test/cctest/test_inspector_socket.cc | 8 ++++---- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 997c3ba00d6..aab07945786 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -1750,7 +1750,7 @@ void SetServers(const FunctionCallbackInfo& 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; diff --git a/src/inspector_socket.cc b/src/inspector_socket.cc index f36682b9338..fbaa4969164 100644 --- a/src/inspector_socket.cc +++ b/src/inspector_socket.cc @@ -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(&input[0]), input.size(), - reinterpret_cast(hash))); + USE(SHA1(reinterpret_cast(input.data()), + input.size(), + reinterpret_cast(hash))); node::base64_encode(hash, sizeof(hash), *buffer, sizeof(*buffer)); } diff --git a/src/inspector_socket_server.cc b/src/inspector_socket_server.cc index 299664da9a1..f8f6a1b4ab6 100644 --- a/src/inspector_socket_server.cc +++ b/src/inspector_socket_server.cc @@ -136,7 +136,7 @@ void SendProtocolJson(InspectorSocket* socket) { strm.next_in = const_cast(PROTOCOL_JSON + 3); strm.avail_in = sizeof(PROTOCOL_JSON) - 3; std::string data(kDecompressedSize, '\0'); - strm.next_out = reinterpret_cast(&data[0]); + strm.next_out = reinterpret_cast(data.data()); strm.avail_out = data.size(); CHECK_EQ(Z_STREAM_END, inflate(&strm, Z_FINISH)); CHECK_EQ(0, strm.avail_out); diff --git a/src/node.cc b/src/node.cc index 66b7ef5ec53..1806693d4bd 100644 --- a/src/node.cc +++ b/src/node.cc @@ -733,9 +733,9 @@ static ExitCode ProcessGlobalArgsInternal(std::vector* args, std::vector 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); } diff --git a/src/node_file.cc b/src/node_file.cc index ed7ec2902f6..9c69cdfc7ed 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1074,7 +1074,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { } while (static_cast(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. } diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 98da55a9d71..b3c0a90b548 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -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; } diff --git a/src/util.cc b/src/util.cc index c5044015c51..5a0b2234852 100644 --- a/src/util.cc +++ b/src/util.cc @@ -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; } diff --git a/test/cctest/test_inspector_socket.cc b/test/cctest/test_inspector_socket.cc index b351a23002c..2efa0d55d74 100644 --- a/test/cctest/test_inspector_socket.cc +++ b/test/cctest/test_inspector_socket.cc @@ -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',