src: do not get string_view from temp string

The result of std::string().substr() will be destroyed at the end of
expression and creating std::string_view from it results in dangling
pointer.

PR-URL: https://github.com/nodejs/node/pull/53688
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
pull/53826/head
Cheng 2024-07-05 09:51:06 +09:00 committed by Antoine du Hamel
parent 082799debb
commit 3ab8aba478
No known key found for this signature in database
GPG Key ID: 21D900FFDB233756
1 changed files with 3 additions and 4 deletions

View File

@ -269,7 +269,7 @@ std::string PathResolve(Environment* env,
void ToNamespacedPath(Environment* env, BufferValue* path) {
#ifdef _WIN32
if (path->length() == 0) return;
auto resolved_path = node::PathResolve(env, {path->ToStringView()});
std::string resolved_path = node::PathResolve(env, {path->ToStringView()});
if (resolved_path.size() <= 2) {
return;
}
@ -282,14 +282,13 @@ void ToNamespacedPath(Environment* env, BufferValue* path) {
if (resolved_path[2] != '?' && resolved_path[2] != '.') {
// Matched non-long UNC root, convert the path to a long UNC path
std::string_view unc_prefix = R"(\\?\UNC\)";
std::string_view resolved_path2 = resolved_path.substr(2);
size_t new_length = unc_prefix.size() + resolved_path2.size();
size_t new_length = unc_prefix.size() + resolved_path.size() - 2;
path->AllocateSufficientStorage(new_length + 1);
path->SetLength(new_length);
memcpy(path->out(), unc_prefix.data(), unc_prefix.size());
memcpy(path->out() + unc_prefix.size(),
resolved_path.c_str() + 2,
resolved_path2.size() + 1);
resolved_path.size() - 2 + 1);
return;
}
}