From 7daf5e001c86a5f7a5fedabef916f5e382989917 Mon Sep 17 00:00:00 2001 From: Callum Moffat Date: Thu, 1 Jun 2023 15:37:16 -0400 Subject: [PATCH] Fix crash getting spell-check suggestions (flutter/engine#42466) On some Samsung devices Flutter with spell-check enabled will crash when typing/moving near the ">" character. Stack trace is ``` Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.textservice.SentenceSuggestionsInfo.getSuggestionsCount()' on a null object reference at io.flutter.plugin.editing.SpellCheckPlugin.onGetSentenceSuggestions(SpellCheckPlugin.java:26) at android.view.textservice.SpellCheckerSession.lambda$handleOnGetSentenceSuggestionsMultiple$1$android-view-textservice-SpellCheckerSession(SpellCheckerSession.java:224) at android.view.textservice.SpellCheckerSession$$ExternalSyntheticLambda0.run(:4) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8747) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) ``` --- .../plugin/editing/SpellCheckPlugin.java | 5 +++++ .../plugin/editing/SpellCheckPluginTest.java | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/SpellCheckPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/SpellCheckPlugin.java index 5688e4fd59..37ee23cfe6 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/SpellCheckPlugin.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/SpellCheckPlugin.java @@ -132,6 +132,11 @@ public class SpellCheckPlugin ArrayList> spellCheckerSuggestionSpans = new ArrayList>(); SentenceSuggestionsInfo spellCheckResults = results[0]; + if (spellCheckResults == null) { + pendingResult.success(new ArrayList>()); + pendingResult = null; + return; + } for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) { SuggestionsInfo suggestionsInfo = spellCheckResults.getSuggestionsInfoAt(i); diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/SpellCheckPluginTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/SpellCheckPluginTest.java index 97a28a0ca2..f5c1a6fb6e 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/SpellCheckPluginTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/SpellCheckPluginTest.java @@ -247,4 +247,22 @@ public class SpellCheckPluginTest { verify(mockResult).success(new ArrayList>()); } + + @Test + public void onGetSentenceSuggestionsResultsWithSuccessAndNoResultsWhenSuggestionsAreInvalid2() { + TextServicesManager fakeTextServicesManager = mock(TextServicesManager.class); + SpellCheckChannel fakeSpellCheckChannel = mock(SpellCheckChannel.class); + SpellCheckPlugin spellCheckPlugin = + spy(new SpellCheckPlugin(fakeTextServicesManager, fakeSpellCheckChannel)); + MethodChannel.Result mockResult = mock(MethodChannel.Result.class); + spellCheckPlugin.pendingResult = mockResult; + + spellCheckPlugin.onGetSentenceSuggestions( + new SentenceSuggestionsInfo[] { + // This "suggestion" may be provided by the Samsung spell checker: + null + }); + + verify(mockResult).success(new ArrayList>()); + } }