Don't trap on an unknown AVSpeechSynthesisVoiceGender - #649
Open
ronjb wants to merge 1 commit into
Open
Conversation
AVSpeechSynthesisVoiceGender is an Objective-C enum, so speechVoices() can return a voice whose gender is a case the app was not compiled against. The switch in stringValue covered only male, female and unspecified, so such a voice made getVoices trap in _diagnoseUnexpectedEnumCaseValue and crash the app. Seen in the wild on iOS 15.8.8 with rawValue 3: Fatal error: unexpected enum case 'AVSpeechSynthesisVoiceGender(rawValue: 3)' SwiftFlutterTtsPlugin.getVoices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The crash
getVoicesreadsvoice.gender.stringValuefor every installed voice.AVSpeechSynthesisVoiceGenderis an Objective-C enum, soAVSpeechSynthesisVoice.speechVoices()can hand back a voice whose gender is a case the app was not compiled against. TheswitchinstringValuecovers only.male,.femaleand.unspecifiedand has no@unknown default, so Swift traps in_diagnoseUnexpectedEnumCaseValueand the whole app goes down.Here is a production crash from an iPad Air 2 on iOS 15.8.8, reporting
rawValue: 3:Two things make this worse than a normal bug:
fatalErroris a process trap, not aPlatformException, so wrappingflutterTts.getVoicesintry/catchdoes nothing.The fix
Add
@unknown defaultreturning"unspecified", which is already the documented fallback on the Dart side. The macOS plugin carries an identical extension and gets the same change.Verified with
swiftc -typecheck -warnings-as-errorsagainst the iOS SDK: the enum is non-frozen, so the new case compiles clean with no "will never be executed" warning.