Verify the length crc8, and stop eager work in the record decoders - #20
Conversation
The reassembler acted on the declared length without checking the crc8 that protects it, so one corrupted length byte consumed up to 4092 bytes of good stream. It now resyncs instead, and exposes a resync count. R24.rawTail is encoded on first read rather than at parse time; it has no readers today and dominated parseR24. hexToBytes uses a nibble lookup over code units, keeping the FormatException callers depend on.
|
Warning Review limit reached
Next review available in: 40 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughFrame reassembly now validates length CRCs and reports resynchronizations. Hex decoding uses explicit nibble parsing. R24 stores raw tail bytes and lazily derives hexadecimal output. Tests cover malformed hex and frame recovery. ChangesProtocol decoding updates
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
test/decode_guards_test.dart (1)
392-403: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd odd-length hexadecimal regression cases.
The new tests cover invalid characters but not incomplete bytes such as
'a'or'abc'. Add them to ensurehexToBytescannot silently discard the final nibble.Proposed test update
- for (final bad in ['0g', 'zz', '0x', '00 11']) { + for (final bad in ['0g', 'zz', '0x', '00 11', 'a', 'abc']) { expect(() => hexToBytes(bad), throwsFormatException, reason: bad); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/decode_guards_test.dart` around lines 392 - 403, Extend the invalid-input cases in the hexToBytes test group to include odd-length hexadecimal strings such as “a” and “abc”, and assert that each throws FormatException. Keep the existing invalid-character coverage unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/src/live.dart`:
- Around line 71-84: Update hexToBytes in lib/src/live.dart (lines 71-84) to
throw FormatException when trimmed.length.isOdd before allocating the output
buffer, while preserving existing invalid-nibble handling. Extend the
malformed-input cases in test/decode_guards_test.dart (lines 392-403) to include
both "a" and "abc".
In `@lib/src/records.dart`:
- Around line 80-81: Copy the incoming rawTailBytes in the R24 constructor
before storing it in _rawTailBytes, so lazy rawTail access and serialization
retain the bytes captured during parsing. Apply the same defensive copy at both
constructor initialization sites referenced by R24._parseV25 and
_parseV24Layout, preserving the existing Uint8List contents while preventing
caller-buffer mutations.
---
Nitpick comments:
In `@test/decode_guards_test.dart`:
- Around line 392-403: Extend the invalid-input cases in the hexToBytes test
group to include odd-length hexadecimal strings such as “a” and “abc”, and
assert that each throws FormatException. Keep the existing invalid-character
coverage unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c4568705-71b3-4bfe-bdc0-448ac7d86c15
📒 Files selected for processing (5)
lib/src/framing.dartlib/src/live.dartlib/src/records.darttest/decode_guards_test.darttest/framing_test.dart
hexToBytes floored the byte count, so a truncated record decoded cleanly at the wrong length instead of failing. rawTail is encoded lazily now, so holding a view into the caller's buffer meant a later mutation could change bytes that were meant to be a snapshot of parse time. It copies instead; the test fails against the view version.
Reassembler trusted an unverified length. It read the declared length and
consumed that many bytes without checking the crc8 that exists to protect it,
so one corrupted length byte discarded up to 4092 bytes of good stream —
records the band is about to trim from flash and will not send again. It now
resyncs one byte instead, and exposes a
resyncscount so a degraded link isstill visible to the caller's corruption detector.
R24.rawTailis lazy. It hex-encoded 83 bytes per record at parse time fora field with no readers anywhere in the stack.
hexToBytesuses a nibble lookup over code units instead ofsubstring+int.parseper byte. It still throwsFormatExceptiononnon-hex input — live.dart, substrate.dart, db.dart and ble_engine.dart all
treat that as "this string is not a record", and returning 0 would hand them
fabricated bytes.
Tests: 110 pass. The 2934-case parity oracle is unchanged. New cases cover a
one-bit length corruption and a dropped byte, both of which previously ate the
following frame.
Summary by CodeRabbit
Bug Fixes
Improvements