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 authored and Michaël Zasso committed 2023-05-12 11:57:01 +02:00
1 parent 456fca0d9c
commit b04d51a0b5
8 files changed
+15 -14

No files matched your search

+1 -1
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;
}