Skip to content

Commit

Permalink
src: use std::u8string instead of std::string to pass path constructor
Browse files Browse the repository at this point in the history
Enforce that it is u8 because it was crashing
when loading files containing Japanese in Windows, cp392 environment.
Fixes: nodejs#56650
  • Loading branch information
yamachu committed Jan 19, 2025
1 parent 6f946c9 commit 8f2c277
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ static void ReportFatalException(Environment* env,
FPrintF(stderr,
"(Use `%s --trace-uncaught ...` to show where the exception "
"was thrown)\n",
filesystem_path.filename().string());
filesystem_path.filename().string().c_str());
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,14 @@ void BindingData::GetNearestParentPackageJSON(
ToNamespacedPath(realm->env(), &path_value);

std::string path_value_str = path_value.ToString();
auto path_value_u8str =
std::u8string(path_value_str.begin(), path_value_str.end());
if (slashCheck) {
path_value_str.push_back(kPathSeparator);
path_value_u8str.push_back(kPathSeparator);
}

auto package_json =
TraverseParent(realm, std::filesystem::path(path_value_str));
TraverseParent(realm, std::filesystem::path(path_value_u8str));

if (package_json != nullptr) {
args.GetReturnValue().Set(package_json->Serialize(realm));
Expand All @@ -366,12 +368,14 @@ void BindingData::GetNearestParentPackageJSONType(
ToNamespacedPath(realm->env(), &path_value);

std::string path_value_str = path_value.ToString();
auto path_value_u8str =
std::u8string(path_value_str.begin(), path_value_str.end());
if (slashCheck) {
path_value_str.push_back(kPathSeparator);
path_value_u8str.push_back(kPathSeparator);
}

auto package_json =
TraverseParent(realm, std::filesystem::path(path_value_str));
TraverseParent(realm, std::filesystem::path(path_value_u8str));

if (package_json == nullptr) {
return;
Expand Down

0 comments on commit 8f2c277

Please sign in to comment.