Skip to content

Commit 097a528

Browse files
authoredJul 22, 2024··
src: do not save c_str of a temp string
PR-URL: #53941 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 2591638 commit 097a528

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed
 

‎src/node_file.cc

+13-13
Original file line numberDiff line numberDiff line change
@@ -1677,33 +1677,33 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
16771677
maxRetries--;
16781678
}
16791679

1680-
// This is required since std::filesystem::path::c_str() returns different
1681-
// values in Windows and Unix.
1680+
// On Windows path::c_str() returns wide char, convert to std::string first.
1681+
std::string file_path_str = file_path.string();
1682+
const char* path_c_str = file_path_str.c_str();
16821683
#ifdef _WIN32
1683-
auto file_ = file_path.string().c_str();
16841684
int permission_denied_error = EPERM;
16851685
#else
1686-
auto file_ = file_path.c_str();
16871686
int permission_denied_error = EACCES;
16881687
#endif // !_WIN32
16891688

16901689
if (error == std::errc::operation_not_permitted) {
1691-
std::string message = "Operation not permitted: " + file_path.string();
1692-
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), file_);
1690+
std::string message = "Operation not permitted: " + file_path_str;
1691+
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), path_c_str);
16931692
} else if (error == std::errc::directory_not_empty) {
1694-
std::string message = "Directory not empty: " + file_path.string();
1695-
return env->ThrowErrnoException(EACCES, "rm", message.c_str(), file_);
1693+
std::string message = "Directory not empty: " + file_path_str;
1694+
return env->ThrowErrnoException(EACCES, "rm", message.c_str(), path_c_str);
16961695
} else if (error == std::errc::not_a_directory) {
1697-
std::string message = "Not a directory: " + file_path.string();
1698-
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), file_);
1696+
std::string message = "Not a directory: " + file_path_str;
1697+
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), path_c_str);
16991698
} else if (error == std::errc::permission_denied) {
1700-
std::string message = "Permission denied: " + file_path.string();
1699+
std::string message = "Permission denied: " + file_path_str;
17011700
return env->ThrowErrnoException(
1702-
permission_denied_error, "rm", message.c_str(), file_);
1701+
permission_denied_error, "rm", message.c_str(), path_c_str);
17031702
}
17041703

17051704
std::string message = "Unknown error: " + error.message();
1706-
return env->ThrowErrnoException(UV_UNKNOWN, "rm", message.c_str(), file_);
1705+
return env->ThrowErrnoException(
1706+
UV_UNKNOWN, "rm", message.c_str(), path_c_str);
17071707
}
17081708

17091709
int MKDirpSync(uv_loop_t* loop,

0 commit comments

Comments
 (0)
Please sign in to comment.