Skip to content

Commit 872d68d

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 <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent bcb35c3 commit 872d68d

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
@@ -3146,21 +3146,8 @@ static void GetFormatOfExtensionlessFile(
31463146
}
31473147

31483148
#ifdef _WIN32
3149-
std::wstring ConvertToWideString(const std::string& str) {
3150-
int size_needed = MultiByteToWideChar(
3151-
CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
3152-
std::wstring wstrTo(size_needed, 0);
3153-
MultiByteToWideChar(CP_UTF8,
3154-
0,
3155-
&str[0],
3156-
static_cast<int>(str.size()),
3157-
&wstrTo[0],
3158-
size_needed);
3159-
return wstrTo;
3160-
}
3161-
31623149
#define BufferValueToPath(str) \
3163-
std::filesystem::path(ConvertToWideString(str.ToString()))
3150+
std::filesystem::path(ConvertToWideString(str.ToString(), CP_UTF8))
31643151

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

src/node_modules.cc

+20-4
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,16 @@ void BindingData::GetNearestParentPackageJSON(
349349
path_value_str.push_back(kPathSeparator);
350350
}
351351

352-
auto package_json =
353-
TraverseParent(realm, std::filesystem::path(path_value_str));
352+
std::filesystem::path path;
353+
354+
#ifdef _WIN32
355+
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
356+
path = std::filesystem::path(wide_path);
357+
#else
358+
path = std::filesystem::path(path_value_str);
359+
#endif
360+
361+
auto package_json = TraverseParent(realm, path);
354362

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

378-
auto package_json =
379-
TraverseParent(realm, std::filesystem::path(path_value_str));
386+
std::filesystem::path path;
387+
388+
#ifdef _WIN32
389+
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
390+
path = std::filesystem::path(wide_path);
391+
#else
392+
path = std::filesystem::path(path_value_str);
393+
#endif
394+
395+
auto package_json = TraverseParent(realm, path);
380396

381397
if (package_json == nullptr) {
382398
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)