Skip to content

Fix crashes on malformed TTS input - #5

Open
thomasbuilds wants to merge 4 commits into
GrapheneOS:mainfrom
thomasbuilds:fix
Open

thomasbuilds wants to merge 4 commits into
GrapheneOS:mainfrom
thomasbuilds:fix

Conversation

@thomasbuilds

@thomasbuilds thomasbuilds commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Malformed locale (LocaleConversion)

Locale.Builder setters throw IllformedLocaleException on ill-formed input; GetSampleTextActivity forwards unvalidated intent extras, so language="x" crashed the process. Each setter is now guarded.

Cleanup

Removed unused SpeechServicesTheme / Typography (no Compose UI). compileDebugKotlin passes with no new detekt/ktlint findings.

other commits stem from conversation below

@thestinger

Copy link
Copy Markdown
Member

The resource leaks should be fixed now.

@thomasbuilds thomasbuilds changed the title Fix crashes on malformed TTS input and an ONNX Runtime memory leak Fix crashes on malformed TTS input May 29, 2026
Comment thread app/src/main/java/app/grapheneos/speechservices/g2p/EnglishPhonemizer.kt Outdated
Comment thread app/src/main/java/app/grapheneos/speechservices/g2p/EnglishPhonemizer.kt Outdated
Comment thread app/src/main/java/app/grapheneos/speechservices/g2p/EnglishPhonemizer.kt Outdated
Comment thread app/src/main/java/app/grapheneos/speechservices/g2p/EnglishPhonemizer.kt Outdated
@thomasbuilds

Copy link
Copy Markdown
Contributor Author

@RankoR thanks for the suggestions but I've seen @thestinger merged the commit you were reviewing.
At this point it's preferable you open a PR yourself if you deem those suggestions still necessary?

@thestinger

Copy link
Copy Markdown
Member

@thomasbuilds It would still be good to change those things, I just wanted to do a SpeechServices v2 release right away to get the fixes out prior to our next OS release.

@RankoR

RankoR commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

@thomasbuilds we need to decide what to do with the very-large-numbers issue. I will return with the decision a bit later

@thomasbuilds

Copy link
Copy Markdown
Contributor Author

Seems like the real limit here isn't Long but ICU's spell-out range. ICU en only spells up to 999,999,999,999,999,999 (10^18 - 1), past that it returns grouped digits like "1,000,000,000,000,000,000", which extendNum splits on [^a-z]+ and drops, so the number isn't voiced.

That gives two broken ranges: [10^18, Long.MAX] already comes out as silent dollars even though it fits in Long, and only above Long.MAX do we get the ?: 0L that says "zero dollars". BigInteger wouldn't help, since ICU still returns digits past 10^18, so it'd just turn "zero dollars" into silent dollars.

So fix would be to spell each part from its original digit string instead of round-tripping through Long and read the digits one by one when numToWords comes back with no letters. The Long is only needed for the zero drop and singular/plural checks, where null can count as non-zero and plural. So $999…9 becomes "nine nine nine … dollars", and the silent [10^18, Long.MAX] band gets read out too.

@thomasbuilds
thomasbuilds requested a review from RankoR June 15, 2026 07:54
@RankoR
RankoR requested a review from soupslurpr June 15, 2026 09:22
thomasbuilds and others added 4 commits June 15, 2026 13:44
Locale.Builder setters throw IllformedLocaleException on ill-formed input. GetSampleTextActivity is exported and passes unvalidated extras through, so catch per-field and skip instead of crashing.
SpeechServicesTheme and Typography are never referenced; no Compose UI exists in the app.
Extract link-target feature parsing into parseFeatureValue() and whitespace
tokenization into an addNonEmptyWords() helper backed by a single compiled
regex. Behavior-preserving.

Co-authored-by: Artem Smirnov <artem@smirnov.page>
numToWords only spells values below 10^18. Beyond that ICU returns
grouped digits, or "infinite" once the string overflows Double, and
extendNum voices neither. The old code therefore dropped amounts in
[10^18, Long.MAX] and read amounts above Long.MAX as "zero dollars".

Spell each part from its original digit string, reading the digits one
by one when the amount overflows Long or numToWords returns no letters.
@thomasbuilds

Copy link
Copy Markdown
Contributor Author

Removed the ktlintCheck fix commit since it got fixed already.

@thomasbuilds

Copy link
Copy Markdown
Contributor Author

kindly ping, some of these commits, particularly af1ea3a suggested by @RankoR, could be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants