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 31, 2024
1 parent 4c844a2 commit 52d0c3a
Show file tree
Hide file tree
Showing 4 changed files with 25 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.ToWString());

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.ToWString());

bool dereference = args[2]->IsTrue();
bool recursive = args[3]->IsTrue();
Expand Down
12 changes: 12 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ void DumpJavaScriptBacktrace(FILE* fp);
#define ABORT_NO_BACKTRACE() abort()
#endif

#ifdef _WIN32
#include <windows.h>
#endif

// Caller of this macro must not be marked as [[noreturn]]. Printing of
// backtraces may not work correctly in [[noreturn]] functions because
// when generating code for them the compiler can choose not to
Expand Down Expand Up @@ -562,6 +566,14 @@ class BufferValue : public MaybeStackBuffer<char> {
inline std::string_view ToStringView() const {
return std::string_view(out(), length());
}
inline std::wstring ToWString() const {
#ifdef _WIN32
auto size_needed = MultiByteToWideChar(CP_UTF8, 0, out(), (int)length(), NULL, 0);
std::wstring wstrTo(length(), 0);
MultiByteToWideChar(CP_UTF8, 0, out(), (int)length(), &wstrTo[0], size_needed);
return wstrTo;
#endif;
}
};

#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 52d0c3a

Please sign in to comment.