Skip to content

Commit

Permalink
src: fix to generate path from wchar_t via wstring
Browse files Browse the repository at this point in the history
Take a similar approach to node_file and allow the creation of paths
code point must be specified to convert from wchar_t to utf8.

PR-URL: #56696
Fixes: #56650
Refs: #56657
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
yamachu authored and lpinca committed Jan 25, 2025
1 parent 687be59 commit 1760024
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
15 changes: 1 addition & 14 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3146,21 +3146,8 @@ static void GetFormatOfExtensionlessFile(
}

#ifdef _WIN32
std::wstring ConvertToWideString(const std::string& str) {
int size_needed = MultiByteToWideChar(
CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8,
0,
&str[0],
static_cast<int>(str.size()),
&wstrTo[0],
size_needed);
return wstrTo;
}

#define BufferValueToPath(str) \
std::filesystem::path(ConvertToWideString(str.ToString()))
std::filesystem::path(ConvertToWideString(str.ToString(), CP_UTF8))

std::string ConvertWideToUTF8(const std::wstring& wstr) {
if (wstr.empty()) return std::string();
Expand Down
24 changes: 20 additions & 4 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,16 @@ void BindingData::GetNearestParentPackageJSON(
path_value_str.push_back(kPathSeparator);
}

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

#ifdef _WIN32
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
path = std::filesystem::path(wide_path);
#else
path = std::filesystem::path(path_value_str);
#endif

auto package_json = TraverseParent(realm, path);

if (package_json != nullptr) {
args.GetReturnValue().Set(package_json->Serialize(realm));
Expand All @@ -375,8 +383,16 @@ void BindingData::GetNearestParentPackageJSONType(
path_value_str.push_back(kPathSeparator);
}

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

#ifdef _WIN32
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
path = std::filesystem::path(wide_path);
#else
path = std::filesystem::path(path_value_str);
#endif

auto package_json = TraverseParent(realm, path);

if (package_json == nullptr) {
return;
Expand Down
16 changes: 16 additions & 0 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,22 @@ bool IsWindowsBatchFile(const char* filename) {
#endif // _WIN32
}

#ifdef _WIN32
inline std::wstring ConvertToWideString(const std::string& str,
UINT code_page) {
int size_needed = MultiByteToWideChar(
code_page, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(code_page,
0,
&str[0],
static_cast<int>(str.size()),
&wstrTo[0],
size_needed);
return wstrTo;
}
#endif // _WIN32

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down

0 comments on commit 1760024

Please sign in to comment.