Skip to content

Commit 7f75746

Browse files
committed
fs: fix cpSync directory iterator exception
This patch prevents process abort when fs.cpSync() encounters errors while iterating directories with non-ASCII characters in paths. Previously, std::filesystem::directory_iterator was constructed without an error_code parameter, causing it to throw std::filesystem::filesystem_error on failure. When the underlying libc++ implementation (especially in Electron) has bugs handling GBK-encoded paths on Windows, this uncaught exception triggers __libcpp_verbose_abort(), causing the process to crash. The crash cannot be caught by JavaScript try-catch blocks since it occurs at the C++ layer below the Node.js binding. This fix changes the directory_iterator construction to use the error_code overload, consistent with other std::filesystem operations in this file. If an error occurs, it is properly converted to a JavaScript exception via env->ThrowStdErrException(), allowing user code to handle it gracefully.
1 parent bf452bb commit 7f75746

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/node_file.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3738,7 +3738,13 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
37383738
&isolate](std::filesystem::path src,
37393739
std::filesystem::path dest) {
37403740
std::error_code error;
3741-
for (auto dir_entry : std::filesystem::directory_iterator(src)) {
3741+
std::filesystem::directory_iterator dir_it(src, error);
3742+
if (error) {
3743+
auto src_str = ConvertPathToUTF8(src);
3744+
env->ThrowStdErrException(error, "cp", src_str.c_str());
3745+
return false;
3746+
}
3747+
for (auto dir_entry : dir_it) {
37423748
auto dest_file_path = dest / dir_entry.path().filename();
37433749
auto dest_str = ConvertPathToUTF8(dest);
37443750

0 commit comments

Comments
 (0)