src: avoid C strings in more C++ exception throws

Similar to 2e2f4cd095, just missed them in that commit.

PR-URL: https://github.com/nodejs/node/pull/60592
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Anna Henningsen
2025-11-07 16:56:54 +01:00
committed by GitHub
parent ed6569c753
commit 368eb4c430
2 changed files with 20 additions and 11 deletions

View File

@@ -23,6 +23,13 @@ concept StringConvertible = requires(T a) {
a.ToString()
} -> std::convertible_to<std::string>;
};
// For std::filesystem::path and similar types
template <typename T>
concept StringConvertibleFSPathLike = requires(T a) {
{
a.string()
} -> std::convertible_to<std::string>;
};
struct ToStringHelper {
template <typename T>
@@ -30,6 +37,12 @@ struct ToStringHelper {
static std::string Convert(const T& value) {
return value.ToString();
}
template <typename T>
requires(StringConvertibleFSPathLike<T>) && (!StringViewConvertible<T>) &&
(!StringConvertible<T>)
static std::string Convert(const T& value) {
return value.string();
}
template <typename T>
requires StringViewConvertible<T>
static std::string_view Convert(const T& value) {

View File

@@ -3526,12 +3526,10 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
if (!dereference &&
std::filesystem::is_directory(symlink_target) &&
isInsideDir(symlink_target, current_dest_symlink_target)) {
std::string message =
static constexpr const char* message =
"Cannot copy %s to a subdirectory of self %s";
THROW_ERR_FS_CP_EINVAL(env,
message.c_str(),
symlink_target.c_str(),
current_dest_symlink_target.c_str());
THROW_ERR_FS_CP_EINVAL(
env, message, symlink_target, current_dest_symlink_target);
return false;
}
@@ -3540,12 +3538,10 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
// and therefore a broken symlink would be created.
if (std::filesystem::is_directory(dest_file_path) &&
isInsideDir(current_dest_symlink_target, symlink_target)) {
std::string message = "cannot overwrite %s with %s";
static constexpr const char* message =
"cannot overwrite %s with %s";
THROW_ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY(
env,
message.c_str(),
current_dest_symlink_target.c_str(),
symlink_target.c_str());
env, message, current_dest_symlink_target, symlink_target);
return false;
}
@@ -3597,7 +3593,7 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
THROW_ERR_FS_CP_EEXIST(isolate,
"[ERR_FS_CP_EEXIST]: Target already exists: "
"cp returned EEXIST (%s already exists)",
dest_file_path.c_str());
dest_file_path);
return false;
}
env->ThrowStdErrException(error, "cp", dest_str.c_str());