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

Commit b71c4c0

Browse files
Yeaseenaduh95
authored andcommitted
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 9adc87b commit b71c4c0

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
@@ -1820,7 +1820,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
18201820
}
18211821

18221822
// On Windows path::c_str() returns wide char, convert to std::string first.
1823-
std::string file_path_str = file_path.string();
1823+
std::string file_path_str = ConvertPathToUTF8(file_path);
18241824
const char* path_c_str = file_path_str.c_str();
18251825
#ifdef _WIN32
18261826
int permission_denied_error = EPERM;
@@ -1829,14 +1829,14 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
18291829
#endif // !_WIN32
18301830

18311831
if (error == std::errc::operation_not_permitted) {
1832-
std::string message = "Operation not permitted: " + file_path_str;
1832+
std::string message = "Operation not permitted:";
18331833
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), path_c_str);
18341834
} else if (error == std::errc::directory_not_empty) {
1835-
std::string message = "Directory not empty: " + file_path_str;
1835+
std::string message = "Directory not empty:";
18361836
return env->ThrowErrnoException(
18371837
ENOTEMPTY, "rm", message.c_str(), path_c_str);
18381838
} else if (error == std::errc::not_a_directory) {
1839-
std::string message = "Not a directory: " + file_path_str;
1839+
std::string message = "Not a directory:";
18401840
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), path_c_str);
18411841
#ifdef _AIX
18421842
} else if (error == std::errc::permission_denied ||
@@ -1847,7 +1847,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
18471847
#else
18481848
} else if (error == std::errc::permission_denied) {
18491849
#endif
1850-
std::string message = "Permission denied: " + file_path_str;
1850+
std::string message = "Permission denied:";
18511851
return env->ThrowErrnoException(
18521852
permission_denied_error, "rm", message.c_str(), path_c_str);
18531853
}
Lines changed: 69 additions & 0 deletions

0 commit comments

Comments
 (0)