Skip to content

Commit

Permalink
src: do not save c_str of a temp string
Browse files Browse the repository at this point in the history
PR-URL: #53941
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
zcbenz authored Jul 22, 2024
1 parent 2591638 commit 097a528
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1677,33 +1677,33 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
maxRetries--;
}

// This is required since std::filesystem::path::c_str() returns different
// values in Windows and Unix.
// On Windows path::c_str() returns wide char, convert to std::string first.
std::string file_path_str = file_path.string();
const char* path_c_str = file_path_str.c_str();
#ifdef _WIN32
auto file_ = file_path.string().c_str();
int permission_denied_error = EPERM;
#else
auto file_ = file_path.c_str();
int permission_denied_error = EACCES;
#endif // !_WIN32

if (error == std::errc::operation_not_permitted) {
std::string message = "Operation not permitted: " + file_path.string();
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), file_);
std::string message = "Operation not permitted: " + file_path_str;
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), path_c_str);
} else if (error == std::errc::directory_not_empty) {
std::string message = "Directory not empty: " + file_path.string();
return env->ThrowErrnoException(EACCES, "rm", message.c_str(), file_);
std::string message = "Directory not empty: " + file_path_str;
return env->ThrowErrnoException(EACCES, "rm", message.c_str(), path_c_str);
} else if (error == std::errc::not_a_directory) {
std::string message = "Not a directory: " + file_path.string();
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), file_);
std::string message = "Not a directory: " + file_path_str;
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), path_c_str);
} else if (error == std::errc::permission_denied) {
std::string message = "Permission denied: " + file_path.string();
std::string message = "Permission denied: " + file_path_str;
return env->ThrowErrnoException(
permission_denied_error, "rm", message.c_str(), file_);
permission_denied_error, "rm", message.c_str(), path_c_str);
}

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

int MKDirpSync(uv_loop_t* loop,
Expand Down

0 comments on commit 097a528

Please sign in to comment.