[Windows] Fix crash if clipboard is set to null (flutter/engine#40238)

Currently, the framework allows setting the clipboard to `null`. [Platforms handle this case inconsistently](https://github.com/flutter/flutter/issues/121976#issuecomment-1464721716), and we will update the framework to disallow setting the clipboard's data to `null`.

In the meantime, this change makes the Windows embedder resilient to unexpected clipboard messages. If your clipboard data's text is unexpected, you will now get an error instead of crashing your app.

Part of https://github.com/flutter/flutter/issues/121976
This commit is contained in:
Loïc Sharma
2023-03-16 16:52:27 -07:00
committed by GitHub
parent 0433227123
commit 25727539bb
2 changed files with 19 additions and 0 deletions

View File

@@ -369,6 +369,10 @@ void PlatformHandler::HandleMethodCall(
result->Error(kClipboardError, kUnknownClipboardFormatMessage);
return;
}
if (!itr->value.IsString()) {
result->Error(kClipboardError, kUnknownClipboardFormatMessage);
return;
}
SetPlainText(itr->value.GetString(), std::move(result));
} else if (method.compare(kPlaySoundMethod) == 0) {
// Only one string argument is expected.

View File

@@ -37,6 +37,8 @@ static constexpr char kClipboardHasStringsFakeContentTypeMessage[] =
"{\"method\":\"Clipboard.hasStrings\",\"args\":\"text/madeupcontenttype\"}";
static constexpr char kClipboardSetDataMessage[] =
"{\"method\":\"Clipboard.setData\",\"args\":{\"text\":\"hello\"}}";
static constexpr char kClipboardSetDataNullTextMessage[] =
"{\"method\":\"Clipboard.setData\",\"args\":{\"text\":null}}";
static constexpr char kClipboardSetDataUnknownTypeMessage[] =
"{\"method\":\"Clipboard.setData\",\"args\":{\"madeuptype\":\"hello\"}}";
static constexpr char kSystemSoundTypeAlertMessage[] =
@@ -368,6 +370,19 @@ TEST_F(PlatformHandlerTest, ClipboardSetData) {
EXPECT_EQ(result, "[null]");
}
// Regression test for: https://github.com/flutter/flutter/issues/121976
TEST_F(PlatformHandlerTest, ClipboardSetDataTextMustBeString) {
use_engine_with_view();
TestBinaryMessenger messenger;
PlatformHandler platform_handler(&messenger, engine());
std::string result =
SimulatePlatformMessage(&messenger, kClipboardSetDataNullTextMessage);
EXPECT_EQ(result, "[\"Clipboard error\",\"Unknown clipboard format\",null]");
}
TEST_F(PlatformHandlerTest, ClipboardSetDataUnknownType) {
use_engine_with_view();