Skip to content

Commit

Permalink
fs: fix cpSync crash on utf characters
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly committed Aug 30, 2024
1 parent 4f14eb1 commit 1ba6b97
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3130,14 +3130,14 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
ToNamespacedPath(env, &src);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, src.ToStringView());
auto src_path = std::filesystem::path(src.ToStringView());
auto src_path = std::filesystem::path(src.ToStringU8View());

BufferValue dest(isolate, args[1]);
CHECK_NOT_NULL(*dest);
ToNamespacedPath(env, &dest);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView());
auto dest_path = std::filesystem::path(dest.ToStringView());
auto dest_path = std::filesystem::path(dest.ToStringU8View());

bool dereference = args[2]->IsTrue();
bool recursive = args[3]->IsTrue();
Expand Down
4 changes: 4 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ class BufferValue : public MaybeStackBuffer<char> {
inline std::string_view ToStringView() const {
return std::string_view(out(), length());
}
inline std::u8string_view ToStringU8View() const {
return std::u8string_view(reinterpret_cast<const char8_t*>(out()),
length());
}
};

#define SPREAD_BUFFER_ARG(val, name) \
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/copy/utf/新建文件/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
purpose: 'testing copy'
};
8 changes: 8 additions & 0 deletions test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ function nextdir() {

// Synchronous implementation of copy.

// It copies a nested folder containing UTF characters.
{
const src = './test/fixtures/copy/utf/新建文件';
const dest = nextdir();
cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
assertDirEquivalent(src, dest);
}

// It copies a nested folder structure with files and folders.
{
const src = './test/fixtures/copy/kitchen-sink';
Expand Down

0 comments on commit 1ba6b97

Please sign in to comment.