Skip to content

Commit a6c2213

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 399b340 commit a6c2213

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
@@ -3526,12 +3526,10 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
35263526
if (!dereference &&
35273527
std::filesystem::is_directory(symlink_target) &&
35283528
isInsideDir(symlink_target, current_dest_symlink_target)) {
3529-
std::string message =
3529+
static constexpr const char* message =
35303530
"Cannot copy %s to a subdirectory of self %s";
3531-
THROW_ERR_FS_CP_EINVAL(env,
3532-
message.c_str(),
3533-
symlink_target.c_str(),
3534-
current_dest_symlink_target.c_str());
3531+
THROW_ERR_FS_CP_EINVAL(
3532+
env, message, symlink_target, current_dest_symlink_target);
35353533
return false;
35363534
}
35373535

@@ -3540,12 +3538,10 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
35403538
// and therefore a broken symlink would be created.
35413539
if (std::filesystem::is_directory(dest_file_path) &&
35423540
isInsideDir(current_dest_symlink_target, symlink_target)) {
3543-
std::string message = "cannot overwrite %s with %s";
3541+
static constexpr const char* message =
3542+
"cannot overwrite %s with %s";
35443543
THROW_ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY(
3545-
env,
3546-
message.c_str(),
3547-
current_dest_symlink_target.c_str(),
3548-
symlink_target.c_str());
3544+
env, message, current_dest_symlink_target, symlink_target);
35493545
return false;
35503546
}
35513547

@@ -3597,7 +3593,7 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
35973593
THROW_ERR_FS_CP_EEXIST(isolate,
35983594
"[ERR_FS_CP_EEXIST]: Target already exists: "
35993595
"cp returned EEXIST (%s already exists)",
3600-
dest_file_path.c_str());
3596+
dest_file_path);
36013597
return false;
36023598
}
36033599
env->ThrowStdErrException(error, "cp", dest_str.c_str());

0 commit comments

Comments
 (0)