From 1ba6b9796efa1e187711117f3ea3a1537a4e2ad6 Mon Sep 17 00:00:00 2001 From: jazelly Date: Fri, 30 Aug 2024 10:34:26 +0930 Subject: [PATCH] fs: fix cpSync crash on utf characters --- src/node_file.cc | 4 ++-- src/util.h | 4 ++++ .../index.js" | 3 +++ test/parallel/test-fs-cp.mjs | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 "test/fixtures/copy/utf/\346\226\260\345\273\272\346\226\207\344\273\266/index.js" diff --git a/src/node_file.cc b/src/node_file.cc index 8ab92f4e55d6e6..333dc3946cc6fb 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3130,14 +3130,14 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo& 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(); diff --git a/src/util.h b/src/util.h index a6da8720c499df..d1e5f75c86454e 100644 --- a/src/util.h +++ b/src/util.h @@ -562,6 +562,10 @@ class BufferValue : public MaybeStackBuffer { inline std::string_view ToStringView() const { return std::string_view(out(), length()); } + inline std::u8string_view ToStringU8View() const { + return std::u8string_view(reinterpret_cast(out()), + length()); + } }; #define SPREAD_BUFFER_ARG(val, name) \ diff --git "a/test/fixtures/copy/utf/\346\226\260\345\273\272\346\226\207\344\273\266/index.js" "b/test/fixtures/copy/utf/\346\226\260\345\273\272\346\226\207\344\273\266/index.js" new file mode 100644 index 00000000000000..12388b0457bdda --- /dev/null +++ "b/test/fixtures/copy/utf/\346\226\260\345\273\272\346\226\207\344\273\266/index.js" @@ -0,0 +1,3 @@ +module.exports = { + purpose: 'testing copy' +}; diff --git a/test/parallel/test-fs-cp.mjs b/test/parallel/test-fs-cp.mjs index 63bc813ae226c7..987da4430b2659 100644 --- a/test/parallel/test-fs-cp.mjs +++ b/test/parallel/test-fs-cp.mjs @@ -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';