Skip to content

Commit cc2b5b4

Browse files
addaleaxaduh95
authored andcommitted
src: avoid C strings in more C++ exception throws
Similar to 2e2f4cd, just missed them in that commit. PR-URL: #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>
1 parent a1c39f4 commit cc2b5b4

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/debug_utils-inl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,26 @@ concept StringConvertible = requires(T a) {
2323
a.ToString()
2424
} -> std::convertible_to<std::string>;
2525
};
26+
// For std::filesystem::path and similar types
27+
template <typename T>
28+
concept StringConvertibleFSPathLike = requires(T a) {
29+
{
30+
a.string()
31+
} -> std::convertible_to<std::string>;
32+
};
2633

2734
struct ToStringHelper {
2835
template <typename T>
2936
requires(StringConvertible<T>) && (!StringViewConvertible<T>)
3037
static std::string Convert(const T& value) {
3138
return value.ToString();
3239
}
40+
template <typename T>
41+
requires(StringConvertibleFSPathLike<T>) && (!StringViewConvertible<T>) &&
42+
(!StringConvertible<T>)
43+
static std::string Convert(const T& value) {
44+
return value.string();
45+
}
3346
template <typename T>
3447
requires StringViewConvertible<T>
3548
static std::string_view Convert(const T& value) {

src/node_file.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3532,12 +3532,10 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
35323532
if (!dereference &&
35333533
std::filesystem::is_directory(symlink_target) &&
35343534
isInsideDir(symlink_target, current_dest_symlink_target)) {
3535-
std::string message =
3535+
static constexpr const char* message =
35363536
"Cannot copy %s to a subdirectory of self %s";
3537-
THROW_ERR_FS_CP_EINVAL(env,
3538-
message.c_str(),
3539-
symlink_target.c_str(),
3540-
current_dest_symlink_target.c_str());
3537+
THROW_ERR_FS_CP_EINVAL(
3538+
env, message, symlink_target, current_dest_symlink_target);
35413539
return false;
35423540
}
35433541

@@ -3546,12 +3544,10 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
35463544
// and therefore a broken symlink would be created.
35473545
if (std::filesystem::is_directory(dest_file_path) &&
35483546
isInsideDir(current_dest_symlink_target, symlink_target)) {
3549-
std::string message = "cannot overwrite %s with %s";
3547+
static constexpr const char* message =
3548+
"cannot overwrite %s with %s";
35503549
THROW_ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY(
3551-
env,
3552-
message.c_str(),
3553-
current_dest_symlink_target.c_str(),
3554-
symlink_target.c_str());
3550+
env, message, current_dest_symlink_target, symlink_target);
35553551
return false;
35563552
}
35573553

@@ -3603,7 +3599,7 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
36033599
THROW_ERR_FS_CP_EEXIST(isolate,
36043600
"[ERR_FS_CP_EEXIST]: Target already exists: "
36053601
"cp returned EEXIST (%s already exists)",
3606-
dest_file_path.c_str());
3602+
dest_file_path);
36073603
return false;
36083604
}
36093605
env->ThrowStdErrException(error, "cp", dest_str.c_str());

0 commit comments

Comments
 (0)