Skip to content

Fix CAT meter parser crash on non-numeric rig replies#489

Merged
patrickrb merged 1 commit into
devfrom
optio/task-bfdd0a1c-a747-47d6-9877-6d5e795735a0
Jul 9, 2026
Merged

Fix CAT meter parser crash on non-numeric rig replies#489
patrickrb merged 1 commit into
devfrom
optio/task-bfdd0a1c-a747-47d6-9877-6d5e795735a0

Conversation

@patrickrb

Copy link
Copy Markdown
Owner

Root cause

The numeric CAT meter parsers validate the reply's token length but never that the substring is actually numeric before calling Integer.parseInt:

  • Yaesu3Command.getALCOrSWR38parseInt(data.substring(1,4)) (FTDX/FT-950 family)
  • Yaesu3Command.getSWROrALC39parseInt(data.substring(1,4)) (FT-891 family)
  • Yaesu3Command.get590ALCOrSWRparseInt(data.substring(1,5)) (Kenwood TS-590/2000/570) — also had no length guard at all
  • ElecraftCommand.getSWRMeterparseInt(data.substring(0,3))

These parsers run per-CAT-poll inside onReceiveData. On the Bluetooth SPP path the reply is dispatched to the parser on the main thread with no surrounding try/catch:

BluetoothSerialService.onSerialRead -> mainLooper.post
  -> BluetoothRigConnector.onSerialRead -> onData
  -> BaseRig.onReceiveData -> <Rig>.onReceiveData -> Yaesu3Command/ElecraftCommand

So a garbled or non-numeric meter reply from the rig threw NumberFormatException (or StringIndexOutOfBoundsException for the unguarded get590ALCOrSWR) and hard-crashed the app. (The USB path is caught by SerialInputOutputManager.run(), which only tears down the CAT thread — hence this bites Bluetooth-connected rigs.)

Fix

Treat malformed meter data as "no reading" (return 0) instead of throwing, matching the existing getFrequency() convention already used in the same two files. Added a length guard to get590ALCOrSWR to match its sibling parsers.

0 is the safe sentinel: it never falsely trips the SWR-halt / ALC auto-volume protection (MeterProtectionController), which key off high values.

Testing

  • New CatMeterParserTest covers numeric, non-numeric, and short data for all four parsers (12 cases). Verified the 6 malformed-data cases fail with NumberFormatException/StringIndexOutOfBoundsException against the old code, and all 12 pass after the fix.
  • Full :app:testDebugUnitTest suite passes.
  • :app:assembleDebug (including native build) succeeds.

Risk

Very low. Behavior is unchanged for valid meter replies (same parsed integer); only malformed/garbled input changes from crash to 0. No protocol, DSP, or threading behavior touched. No device was attached in this environment, so no on-device verification was performed.

🤖 Generated with Claude Code

The numeric CAT meter parsers on Yaesu3Command (getALCOrSWR38,
getSWROrALC39, get590ALCOrSWR) and ElecraftCommand (getSWRMeter) call
Integer.parseInt on a fixed substring of the rig's reply, validating only
the token length and never that the substring is numeric. get590ALCOrSWR
additionally had no length guard at all.

These parsers run per-CAT-poll inside onReceiveData. On the Bluetooth SPP
path (BluetoothSerialService -> BluetoothRigConnector.onSerialRead ->
BaseRig.onReceiveData) that runs on the main thread with no surrounding
try/catch, so a garbled or non-numeric meter reply threw
NumberFormatException (or StringIndexOutOfBoundsException for the
unguarded get590ALCOrSWR) and hard-crashed the app.

Treat malformed meter data as 'no reading' (0) instead of throwing,
matching the existing getFrequency() convention in the same files. 0 is
the safe sentinel: it never falsely trips the SWR-halt / ALC protection,
which key off high values.

Add CatMeterParserTest covering numeric, non-numeric, and short data for
all four parsers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 23.35%. Comparing base (d71f162) to head (d319bdd).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##                dev     #489   +/-   ##
=========================================
  Coverage     23.34%   23.35%           
- Complexity      149      162   +13     
=========================================
  Files           165      167    +2     
  Lines         21401    21450   +49     
  Branches       3149     3154    +5     
=========================================
+ Hits           4997     5009   +12     
- Misses        16215    16248   +33     
- Partials        189      193    +4     
Flag Coverage Δ
android 13.26% <ø> (+0.03%) ⬆️
native 9.93% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request hardens CAT meter parsing against malformed rig replies to prevent main-thread crashes (notably on the Bluetooth SPP path), while preserving behavior for valid numeric meter data.

Changes:

  • Add numeric parsing guards (length checks + NumberFormatException handling) for Yaesu/Kenwood meter parsers in Yaesu3Command.
  • Add length check + NumberFormatException handling for Elecraft SWR meter parsing in ElecraftCommand.
  • Introduce a new unit test suite (CatMeterParserTest) covering valid, non-numeric, and short meter replies across the affected parsers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
ft8af/app/src/main/java/com/k1af/ft8af/rigs/Yaesu3Command.java Prevents crashes by guarding substring bounds and safely parsing meter tokens via a shared helper that returns 0 on malformed input.
ft8af/app/src/main/java/com/k1af/ft8af/rigs/ElecraftCommand.java Adds length check and safe parsing to avoid NumberFormatException crashes on malformed SWR replies.
ft8af/app/src/test/java/com/k1af/ft8af/rigs/CatMeterParserTest.java Adds focused unit coverage for all affected meter parsers across valid and malformed inputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@patrickrb patrickrb merged commit 0a12f91 into dev Jul 9, 2026
18 checks passed
@patrickrb patrickrb deleted the optio/task-bfdd0a1c-a747-47d6-9877-6d5e795735a0 branch July 9, 2026 02:14
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.

2 participants