Skip to content

Commit

Permalink
src: do not get string_view from temp string
Browse files Browse the repository at this point in the history
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: nodejs#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>
  • Loading branch information
zcbenz committed Jul 5, 2024
1 parent 60b2754 commit 2fd798d
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 2fd798d

Please sign in to comment.