Skip to content

Commit ef87dc1

Browse files
authored
Fix null terminator in input truncates clipboard (flutter#188652)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Closes flutter#162226. Sadly win32 doesn't allow `\0` characters inside the `CF_UNICODETEXT` clipboard. To prevent it, I replaced the null terminator with the `\uFFFD` (replacement character: https://www.compart.com/en/unicode/U+FFFD). I replaced the string type to a string view to also store the size on the way so it doesn't break on the way if there are null terminators. ~~Currently a draft since I don't have tests yet.~~ ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 2a2a79d commit ef87dc1

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

‎engine/src/flutter/fml/platform/win/wstring_conversion.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ using WideStringConverter =
1515

1616
std::string WideStringToUtf8(const std::wstring_view str) {
1717
WideStringConverter converter;
18-
return converter.to_bytes(str.data());
18+
return converter.to_bytes(str.data(), str.data() + str.size());
1919
}
2020

2121
std::wstring Utf8ToWideString(const std::string_view str) {
2222
WideStringConverter converter;
23-
return converter.from_bytes(str.data());
23+
return converter.from_bytes(str.data(), str.data() + str.size());
2424
}
2525

2626
std::u16string WideStringToUtf16(const std::wstring_view str) {

‎engine/src/flutter/shell/platform/windows/platform_handler.cc‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ void PlatformHandler::GetHasStrings(
341341
}
342342

343343
void PlatformHandler::SetPlainText(
344-
const std::string& text,
344+
std::string_view text,
345345
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
346346
std::unique_ptr<ScopedClipboardInterface> clipboard =
347347
scoped_clipboard_provider_();
@@ -353,7 +353,11 @@ void PlatformHandler::SetPlainText(
353353
result->Error(kClipboardError, "Unable to open clipboard", error_code);
354354
return;
355355
}
356-
int set_result = clipboard->SetString(fml::Utf8ToWideString(text));
356+
std::wstring clipboard_text = fml::Utf8ToWideString(text);
357+
// Windows clipboard strings are null-terminated, so an embedded null
358+
// character causes other applications to paste only the text before it.
359+
std::replace(clipboard_text.begin(), clipboard_text.end(), L'\0', L'\uFFFD');
360+
int set_result = clipboard->SetString(clipboard_text);
357361
if (set_result != kErrorSuccess) {
358362
rapidjson::Document error_code;
359363
error_code.SetInt(set_result);
@@ -504,7 +508,9 @@ void PlatformHandler::HandleMethodCall(
504508
result->Error(kClipboardError, kUnknownClipboardFormatMessage);
505509
return;
506510
}
507-
SetPlainText(itr->value.GetString(), std::move(result));
511+
SetPlainText(
512+
std::string_view(itr->value.GetString(), itr->value.GetStringLength()),
513+
std::move(result));
508514
} else if (method.compare(kPlaySoundMethod) == 0) {
509515
// Only one string argument is expected.
510516
const rapidjson::Value& sound_type = method_call.arguments()[0];

‎engine/src/flutter/shell/platform/windows/platform_handler.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class PlatformHandler {
6969
// Sets the clipboard's plain text to |text|, and reports the result (either
7070
// an error, or null for success) to |result|.
7171
virtual void SetPlainText(
72-
const std::string& text,
72+
std::string_view text,
7373
std::unique_ptr<MethodResult<rapidjson::Document>> result);
7474

7575
virtual void SystemSoundPlay(

‎engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ static constexpr char kClipboardHasStringsFakeContentTypeMessage[] =
3838
"{\"method\":\"Clipboard.hasStrings\",\"args\":\"text/madeupcontenttype\"}";
3939
static constexpr char kClipboardSetDataMessage[] =
4040
"{\"method\":\"Clipboard.setData\",\"args\":{\"text\":\"hello\"}}";
41+
static constexpr char kClipboardSetDataNullTerminatorMessage[] =
42+
"{\"method\":\"Clipboard.setData\",\"args\":{\"text\":"
43+
"\"hello\\u0000world\"}}";
4144
static constexpr char kClipboardSetDataNullTextMessage[] =
4245
"{\"method\":\"Clipboard.setData\",\"args\":{\"text\":null}}";
4346
static constexpr char kClipboardSetDataUnknownTypeMessage[] =
@@ -82,7 +85,7 @@ class MockPlatformHandler : public PlatformHandler {
8285
(override));
8386
MOCK_METHOD(void,
8487
SetPlainText,
85-
(const std::string&,
88+
(std::string_view,
8689
std::unique_ptr<MethodResult<rapidjson::Document>>),
8790
(override));
8891
MOCK_METHOD(void,
@@ -358,6 +361,33 @@ TEST_F(PlatformHandlerTest, ClipboardSetData) {
358361
EXPECT_EQ(result, "[null]");
359362
}
360363

364+
// Regression test for https://github.com/flutter/flutter/issues/162226.
365+
TEST_F(PlatformHandlerTest, ClipboardSetDataReplacesNullTerminators) {
366+
UseHeadlessEngine();
367+
368+
TestBinaryMessenger messenger;
369+
PlatformHandler platform_handler(&messenger, engine(), []() {
370+
auto clipboard = std::make_unique<MockScopedClipboard>();
371+
372+
EXPECT_CALL(*clipboard.get(), Open)
373+
.Times(1)
374+
.WillOnce(Return(kErrorSuccess));
375+
EXPECT_CALL(*clipboard.get(), SetString)
376+
.Times(1)
377+
.WillOnce([](std::wstring string) {
378+
EXPECT_EQ(string, L"hello\uFFFDworld");
379+
return kErrorSuccess;
380+
});
381+
382+
return clipboard;
383+
});
384+
385+
std::string result = SimulatePlatformMessage(
386+
&messenger, kClipboardSetDataNullTerminatorMessage);
387+
388+
EXPECT_EQ(result, "[null]");
389+
}
390+
361391
// Regression test for: https://github.com/flutter/flutter/issues/121976
362392
TEST_F(PlatformHandlerTest, ClipboardSetDataTextMustBeString) {
363393
UseHeadlessEngine();

0 commit comments

Comments
 (0)