Skip to content

Commit f262837

Browse files
yamachuaduh95
authored andcommitted
src: fix to generate path from wchar_t via wstring
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]>
1 parent 3424c7d commit f262837

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/node_file.cc

+1-14
Original file line numberDiff line numberDiff line change
@@ -3047,21 +3047,8 @@ static void GetFormatOfExtensionlessFile(
30473047
}
30483048

30493049
#ifdef _WIN32
3050-
std::wstring ConvertToWideString(const std::string& str) {
3051-
int size_needed = MultiByteToWideChar(
3052-
CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
3053-
std::wstring wstrTo(size_needed, 0);
3054-
MultiByteToWideChar(CP_UTF8,
3055-
0,
3056-
&str[0],
3057-
static_cast<int>(str.size()),
3058-
&wstrTo[0],
3059-
size_needed);
3060-
return wstrTo;
3061-
}
3062-
30633050
#define BufferValueToPath(str) \
3064-
std::filesystem::path(ConvertToWideString(str.ToString()))
3051+
std::filesystem::path(ConvertToWideString(str.ToString(), CP_UTF8))
30653052

30663053
std::string ConvertWideToUTF8(const std::wstring& wstr) {
30673054
if (wstr.empty()) return std::string();

src/node_modules.cc

+20-4
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,16 @@ void BindingData::GetNearestParentPackageJSON(
344344
path_value_str.push_back(kPathSeparator);
345345
}
346346

347-
auto package_json =
348-
TraverseParent(realm, std::filesystem::path(path_value_str));
347+
std::filesystem::path path;
348+
349+
#ifdef _WIN32
350+
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
351+
path = std::filesystem::path(wide_path);
352+
#else
353+
path = std::filesystem::path(path_value_str);
354+
#endif
355+
356+
auto package_json = TraverseParent(realm, path);
349357

350358
if (package_json != nullptr) {
351359
args.GetReturnValue().Set(package_json->Serialize(realm));
@@ -370,8 +378,16 @@ void BindingData::GetNearestParentPackageJSONType(
370378
path_value_str.push_back(kPathSeparator);
371379
}
372380

373-
auto package_json =
374-
TraverseParent(realm, std::filesystem::path(path_value_str));
381+
std::filesystem::path path;
382+
383+
#ifdef _WIN32
384+
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
385+
path = std::filesystem::path(wide_path);
386+
#else
387+
path = std::filesystem::path(path_value_str);
388+
#endif
389+
390+
auto package_json = TraverseParent(realm, path);
375391

376392
if (package_json == nullptr) {
377393
return;

src/util-inl.h

+16
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,22 @@ bool IsWindowsBatchFile(const char* filename) {
562562
#endif // _WIN32
563563
}
564564

565+
#ifdef _WIN32
566+
inline std::wstring ConvertToWideString(const std::string& str,
567+
UINT code_page) {
568+
int size_needed = MultiByteToWideChar(
569+
code_page, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
570+
std::wstring wstrTo(size_needed, 0);
571+
MultiByteToWideChar(code_page,
572+
0,
573+
&str[0],
574+
static_cast<int>(str.size()),
575+
&wstrTo[0],
576+
size_needed);
577+
return wstrTo;
578+
}
579+
#endif // _WIN32
580+
565581
} // namespace node
566582

567583
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)