Repository navigation
[FML] Replace deprecated wstring_convert with Win32 APIs - #191394
Conversation
Replaced the deprecated `std::wstring_convert` and `<codecvt>` usage in `wstring_conversion.cc` with native Win32 APIs `MultiByteToWideChar` and `WideCharToMultiByte`. This avoids the deprecation warnings and prevents potential correctness bugs (such as slash-collapsing in URLs) and performance overhead associated with `std::filesystem::path`. Added unit tests to verify that URLs are preserved during conversion.
Replaces Win32 APIs in wstring_conversion.cc with C++20 std::filesystem::path conversions.
There was a problem hiding this comment.
Code Review
This pull request replaces the deprecated std::wstring_convert with std::filesystem::path for UTF-8 and UTF-16 string conversions on Windows, and adds new unit tests. The review feedback notes that using std::filesystem::path for general-purpose string conversion can cause unexpected path normalization, potentially corrupting non-path strings like URLs. The reviewer suggests using native Win32 APIs (WideCharToMultiByte and MultiByteToWideChar) instead.
| #include <filesystem> | ||
| #include <string> |
There was a problem hiding this comment.
| std::string WideStringToUtf8(const std::wstring_view str) { | ||
| WideStringConverter converter; | ||
| return converter.to_bytes(str.data(), str.data() + str.size()); | ||
| if (str.empty()) { | ||
| return {}; | ||
| } | ||
| std::filesystem::path path(str); | ||
| std::u8string u8str = path.u8string(); | ||
| return std::string(u8str.begin(), u8str.end()); | ||
| } |
There was a problem hiding this comment.
Using std::filesystem::path for general-purpose string conversion is semantically incorrect and can lead to unexpected behavior. std::filesystem::path is designed specifically for filesystem paths and may perform path normalization (such as converting directory separators / to \ on Windows) or other path-specific validations. This can corrupt non-path strings, such as URLs (e.g., https://flutter.dev could be altered).
Since this file is Windows-specific (platform/win), we should use the standard, high-performance Win32 API WideCharToMultiByte for robust and correct UTF-8/UTF-16 conversions.
std::string WideStringToUtf8(const std::wstring_view str) {
if (str.empty()) {
return {};
}
int size_needed = WideCharToMultiByte(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), NULL, 0, NULL, NULL);
if (size_needed <= 0) {
return {};
}
std::string result(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), &result[0], size_needed, NULL, NULL);
return result;
}There was a problem hiding this comment.
Updated to use WideCharToMultiByte.
| std::wstring Utf8ToWideString(const std::string_view str) { | ||
| WideStringConverter converter; | ||
| return converter.from_bytes(str.data(), str.data() + str.size()); | ||
| if (str.empty()) { | ||
| return {}; | ||
| } | ||
| const char8_t* start = reinterpret_cast<const char8_t*>(str.data()); | ||
| const char8_t* end = start + str.size(); | ||
| std::filesystem::path path(start, end); | ||
| return path.wstring(); | ||
| } |
There was a problem hiding this comment.
Using std::filesystem::path for general-purpose string conversion is semantically incorrect and can lead to unexpected behavior (such as path normalization or separator conversion). Since this is Windows-specific code, we should use the standard Win32 API MultiByteToWideChar.
std::wstring Utf8ToWideString(const std::string_view str) {
if (str.empty()) {
return {};
}
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), NULL, 0);
if (size_needed <= 0) {
return {};
}
std::wstring result(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), &result[0], size_needed);
return result;
}There was a problem hiding this comment.
Updated to use MultiByteToWideChar.
|
So this change is technically correct, but the bot's comments are valid. Do we care if we migrate to using |
gaaclarke
left a comment
There was a problem hiding this comment.
So this change is technically correct, but the bot's comments are valid. Do we care if we migrate to using
<windows.h>APIs from<codecvt>, or should we ignore these comments and just use<filesystem>for consistency?
I think we should take the bots advice here. I think there are subtle ways using paths for nonpaths could mess things up in weird ways. These functions are too generically named, someone could easily use it on accident. I poked around and I don't think there is a clean c++20 way to do this.
Thanks for digging into this, sorry it isn't as straightforward as we'd like.
…g conversion Addresses review feedback by switching to native Win32 APIs (MultiByteToWideChar and WideCharToMultiByte) instead of std::filesystem::path.
Not a problem! I'd rather us get this right than just land whatever works :) |
|
autosubmit label was removed for flutter/flutter/191394, because - The status or check suite Mac_arm64_verify_binaries has failed. Please fix the issues identified (or deflake) before re-applying this label. |
Roll Flutter from c2437523d308 to 65c9a8dc60bc (195 revisions) flutter/flutter@c243752...65c9a8d 2026-08-22 engine-flutter-autoroll@skia.org Roll ICU from d578f2e8b7bd to 8cc91d9b6ab9 (1 revision) (flutter/flutter#191542) 2026-08-22 engine-flutter-autoroll@skia.org Roll Skia from 666a9b3d5cf0 to ad2106c0bb64 (3 revisions) (flutter/flutter#191527) 2026-08-22 engine-flutter-autoroll@skia.org Roll Fuchsia Test Scripts from KaOq3EE4qJ9fnaaaK... to 0iCv10IlKfiilEBOU... (flutter/flutter#191524) 2026-08-22 bkonyi@google.com [flutter_tools] Add tests for negative lookahead regex in test runner and batch entrypoints (flutter/flutter#191438) 2026-08-22 engine-flutter-autoroll@skia.org Roll Skia from 0c37868737fa to 666a9b3d5cf0 (2 revisions) (flutter/flutter#191518) 2026-08-21 10456171+caroqliu@users.noreply.github.com Revert "[input] Migrate fuchsia.ui.pointerinjector to TouchSource (#190855) (flutter/flutter#191509) 2026-08-21 30870216+gaaclarke@users.noreply.github.com Fixes windows gallery benchmarks by forcing mobile layout (flutter/flutter#191507) 2026-08-21 bkonyi@google.com [flutter_tools] Restrict WebAssetServer source resolution to source map extensions (flutter/flutter#191501) 2026-08-21 engine-flutter-autoroll@skia.org Roll Skia from f6900c5b8439 to 0c37868737fa (2 revisions) (flutter/flutter#191504) 2026-08-21 bkonyi@google.com Refactor `FlutterDevice.connect` and VM service discovery (flutter/flutter#191221) 2026-08-21 bkonyi@google.com [flutter_tools] Fix crash when migrating flow-style exclude lists in analysis_options.yaml (flutter/flutter#191269) 2026-08-21 269567208+reidbaker-agent@users.noreply.github.com [rules] Add packages/flutter_tools/gradle/AGENTS.md rules (flutter/flutter#191486) 2026-08-21 kevmoo@users.noreply.github.com [flutter_tools] refactor CLI argument architecture with typed option descriptors and bundles (PoC) (flutter/flutter#191018) 2026-08-21 bkonyi@google.com tools: Extract Dart SDK to temp directory before moving to final location (flutter/flutter#191263) 2026-08-21 1961493+harryterkelsen@users.noreply.github.com [web] Move CanvasKit fragment shader classes to canvaskit/fragment_shader.dart (flutter/flutter#191451) 2026-08-21 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from GCQlmt6h-esJsNubS... to ic6GjOSn-KN508XyK... (flutter/flutter#191485) 2026-08-21 bkonyi@google.com [Widget Preview] Isolate PageStorage scope in widget preview group expansion tile (flutter/flutter#191378) 2026-08-21 bkonyi@google.com [flutter_tools] Deprecate --build and --no-build flags on flutter run (flutter/flutter#191358) 2026-08-21 engine-flutter-autoroll@skia.org Roll Skia from 70988bed1b3b to f6900c5b8439 (2 revisions) (flutter/flutter#191481) 2026-08-21 engine-flutter-autoroll@skia.org Roll Packages from 1785501 to 252bb33 (6 revisions) (flutter/flutter#191480) 2026-08-21 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from 20IJas24bZiNmCZTK... to GCQlmt6h-esJsNubS... (flutter/flutter#191415) 2026-08-21 engine-flutter-autoroll@skia.org Roll Skia from 2ba6971bd0d1 to 70988bed1b3b (2 revisions) (flutter/flutter#191476) 2026-08-21 engine-flutter-autoroll@skia.org Roll Skia from 1d5f72537ba6 to 2ba6971bd0d1 (2 revisions) (flutter/flutter#191473) 2026-08-21 engine-flutter-autoroll@skia.org Roll Skia from 2c25efd2e369 to 1d5f72537ba6 (1 revision) (flutter/flutter#191472) 2026-08-21 engine-flutter-autoroll@skia.org Roll Skia from 09b1b810850a to 2c25efd2e369 (9 revisions) (flutter/flutter#191470) 2026-08-21 flar@google.com [Impeller] fix position of cached single glyph text shadows (flutter/flutter#191325) 2026-08-21 engine-flutter-autoroll@skia.org Roll Skia from abdf8821f313 to 09b1b810850a (15 revisions) (flutter/flutter#191458) 2026-08-21 me@bnsaed.com Document that programmatic TextEditingController changes do not run input formatters (flutter/flutter#190166) 2026-08-21 30870216+gaaclarke@users.noreply.github.com Adds new gallery benchmarks to windows (skia and impeller) (flutter/flutter#191454) 2026-08-21 chris@bracken.jp iOS: Deprecate FlutterEngine.isGpuDisabled (flutter/flutter#191393) 2026-08-21 154381524+flutteractionsbot@users.noreply.github.com Revert: [web] Unskip decoration image lerp tests (flutter/flutter#191462) 2026-08-20 awolff@google.com android_hardware_smoke_test: Improve reliability (flutter/flutter#191374) 2026-08-20 77467499+wilyan09007@users.noreply.github.com Don't size or offset the Android platform view before it is laid out (flutter/flutter#190895) 2026-08-20 15619084+vashworth@users.noreply.github.com [iOS][add2app] Skip building SwiftPM plugins when generating CocoaPods artifacts (flutter/flutter#190736) 2026-08-20 33794642+FelixMittermeier@users.noreply.github.com Optimize JSONMessageCodec UTF-8 conversion (flutter/flutter#190529) 2026-08-20 bkonyi@google.com [FML] Replace deprecated wstring_convert with Win32 APIs (flutter/flutter#191394) 2026-08-20 victorsanniay@gmail.com SliverFillRemaining extends beyond viewport size when fillOverscroll is true (flutter/flutter#191236) 2026-08-20 bkonyi@google.com [flutter_tools] Update argParser usageLineLength when --wrap-column is passed (flutter/flutter#191264) 2026-08-20 bkonyi@google.com [flutter_tools] Fix UNC path resolution in depfile parsing on Windows (flutter/flutter#191265) 2026-08-20 1961493+harryterkelsen@users.noreply.github.com [web] Unskip decoration image lerp tests (flutter/flutter#191426) 2026-08-20 bkonyi@google.com Do not inject 'type' and 'method' into service extension responses (flutter/flutter#190946) 2026-08-20 bkonyi@google.com [flutter_tools] Fix crash in symbolize command on stream error (flutter/flutter#191273) 2026-08-20 bkonyi@google.com [flutter_tools] Prevent deletion of shared native asset hooks outputs as stale (flutter/flutter#191272) 2026-08-20 bkonyi@google.com [flutter_tools] Support package wildcard assets in app pubspec (flutter/flutter#191266) 2026-08-20 108678139+manu-sncf@users.noreply.github.com Add SliverClipRect and SliverClipRRect (flutter/flutter#179003) 2026-08-20 bkonyi@google.com [flutter_tools] Implement Diagnostics extension slice and doctor integration (flutter/flutter#191162) ...
Replaces deprecated
std::wstring_convertand<codecvt>usage inengine/src/flutter/fml/platform/win/wstring_conversion.ccwith native Win32 APIsMultiByteToWideCharandWideCharToMultiByte.Changes
WideStringToUtf8: UsesWideCharToMultiBytewithCP_UTF8.Utf8ToWideString: UsesMultiByteToWideCharwithCP_UTF8.wstring_conversion_unittests.ccto verify string conversions with URLs and Unicode characters.