From 137914a01ce06c7b0242275bca22c6d0b67b649b Mon Sep 17 00:00:00 2001 From: Cheng Date: Tue, 2 Jul 2024 17:18:18 +0900 Subject: [PATCH] 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. --- src/path.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/path.cc b/src/path.cc index 0f7778313a2f3d..fade21c8af9414 100644 --- a/src/path.cc +++ b/src/path.cc @@ -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; } }