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: #53688
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
zcbenz authored and aduh95 committed Jul 16, 2024
1 parent 082799d commit 3ab8aba
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 3ab8aba

Please sign in to comment.