Skip to content

Commit 036e4f2

Browse files
yamachusevenc-nanashi
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: nodejs#56696 Fixes: nodejs#56650 Refs: nodejs#56657 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent e69d35f commit 036e4f2

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/node_file.cc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,21 +3048,8 @@ static void GetFormatOfExtensionlessFile(
30483048
}
30493049

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

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

src/node_modules.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,16 @@ void BindingData::GetNearestParentPackageJSON(
343343
path_value_str.push_back(kPathSeparator);
344344
}
345345

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

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

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

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

src/util-inl.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,22 @@ bool IsWindowsBatchFile(const char* filename) {
557557
#endif // _WIN32
558558
}
559559

560+
#ifdef _WIN32
561+
inline std::wstring ConvertToWideString(const std::string& str,
562+
UINT code_page) {
563+
int size_needed = MultiByteToWideChar(
564+
code_page, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
565+
std::wstring wstrTo(size_needed, 0);
566+
MultiByteToWideChar(code_page,
567+
0,
568+
&str[0],
569+
static_cast<int>(str.size()),
570+
&wstrTo[0],
571+
size_needed);
572+
return wstrTo;
573+
}
574+
#endif // _WIN32
575+
560576
} // namespace node
561577

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

0 commit comments

Comments
 (0)