fs: fix rmSync error messages for non-ASCII paths · nodejs/node@37aeecc · GitHub
Skip to content

Commit 37aeecc

Browse files
authored
fs: fix rmSync error messages for non-ASCII paths
fs.rmSync previously embedded paths directly into custom error messages while also passing the path to ThrowErrnoException. This caused duplicated paths for ASCII names and corrupted paths for non-ASCII directory names on Linux, and inconsistent path formatting on Windows. Remove path concatenation from custom messages and rely on ThrowErrnoException to attach the path safely. Add a test to cover non-ASCII directory names. Signed-off-by: Yeaseen <yeaseen.arafat96@gmail.com> PR-URL: #61233 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 4173ae2 commit 37aeecc

3 files changed

Lines changed: 113 additions & 24 deletions

File tree

‎src/api/exceptions.cc‎

Lines changed: 39 additions & 19 deletions

‎src/node_file.cc‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
18281828
}
18291829

18301830
// On Windows path::c_str() returns wide char, convert to std::string first.
1831-
std::string file_path_str = file_path.string();
1831+
std::string file_path_str = ConvertPathToUTF8(file_path);
18321832
const char* path_c_str = file_path_str.c_str();
18331833
#ifdef _WIN32
18341834
int permission_denied_error = EPERM;
@@ -1837,14 +1837,14 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
18371837
#endif // !_WIN32
18381838

18391839
if (error == std::errc::operation_not_permitted) {
1840-
std::string message = "Operation not permitted: " + file_path_str;
1840+
std::string message = "Operation not permitted:";
18411841
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), path_c_str);
18421842
} else if (error == std::errc::directory_not_empty) {
1843-
std::string message = "Directory not empty: " + file_path_str;
1843+
std::string message = "Directory not empty:";
18441844
return env->ThrowErrnoException(
18451845
ENOTEMPTY, "rm", message.c_str(), path_c_str);
18461846
} else if (error == std::errc::not_a_directory) {
1847-
std::string message = "Not a directory: " + file_path_str;
1847+
std::string message = "Not a directory:";
18481848
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), path_c_str);
18491849
#ifdef _AIX
18501850
} else if (error == std::errc::permission_denied ||
@@ -1855,7 +1855,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
18551855
#else
18561856
} else if (error == std::errc::permission_denied) {
18571857
#endif
1858-
std::string message = "Permission denied: " + file_path_str;
1858+
std::string message = "Permission denied:";
18591859
return env->ThrowErrnoException(
18601860
permission_denied_error, "rm", message.c_str(), path_c_str);
18611861
}
Lines changed: 69 additions & 0 deletions

0 commit comments

Comments
 (0)