From c0b58e6e57fe567924f2fd2b38e6fd026d93bc54 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 5 Dec 2026 13:21:57 +0530 Subject: [PATCH] framing: give-up scan also looks inside a phantom header --- lib/src/framing.dart | 16 ++++++++++------ test/framing_test.dart | 15 ++++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/src/framing.dart b/lib/src/framing.dart index 8e7ab74..b60cc88 100644 --- a/lib/src/framing.dart +++ b/lib/src/framing.dart @@ -236,7 +236,10 @@ class FrameReassembler { final next = _completeFrameWithin(headerLen); if (next < 0) break; // wait for the rest of this frame out.add(Frame( - Uint8List.fromList(_buf.sublist(headerLen, next)), true, false, + Uint8List.fromList( + _buf.sublist(next < headerLen ? next : headerLen, next)), + true, + false, truncated: true)); _resyncs++; _truncations++; @@ -277,12 +280,13 @@ class FrameReassembler { /// Offset of the first SOF after the pending frame's header that starts a /// frame which is fully buffered and passes both CRCs, or -1. /// - /// The scan starts past the header: that header already passed its check, - /// and its crc8 byte is 0xAA for about 1 in 256 lengths, so starting inside - /// it can match a frame at an offset shorter than the header and the caller's - /// payload slice runs backwards. + /// The scan starts at 1, inside the header: the pending header can be a + /// phantom (a stray 0xAA in a corrupted frame's tail that passes the header + /// check), and a real frame can then start within its first headerLen bytes. + /// A match there needs both CRCs, so it is real data either way. The caller + /// must not slice backwards for it. int _completeFrameWithin(int headerLen) { - for (int k = headerLen; k + headerLen + 4 <= _buf.length; k++) { + for (int k = 1; k + headerLen + 4 <= _buf.length; k++) { if (_buf[k] != sof) continue; final header = _buf.sublist(k, k + headerLen); final declared = profile.declaredLen(header); diff --git a/test/framing_test.dart b/test/framing_test.dart index 68b3763..204968c 100644 --- a/test/framing_test.dart +++ b/test/framing_test.dart @@ -244,10 +244,10 @@ void main() { expect(re.truncations, 0); }); - test('give-up does not resume inside the truncated frame\'s own header', + test('give-up finds a real frame that starts inside a phantom header', () { - // A length pair whose crc8 is itself 0xAA, so the header's last byte - // reads as an SOF and a valid frame can start there. + // A phantom header (passes crc8, declares a long length) whose last + // byte is the SOF of a real frame. late int lo, hi; search: for (hi = 0; hi < 16; hi++) { @@ -258,11 +258,12 @@ void main() { final inner = buildFrame([PacketType.event, 1, 0x05, 0, 0, 0, 0, 0]); expect(inner.first, 0xAA); final re = FrameReassembler(giveUpTruncatedFrames: true); - expect(re.feed([0xAA, lo, hi, ...inner]), isEmpty, - reason: 'nothing complete sits after the header, so this waits'); - final frames = re.feed(historyEnd(2)); + final frames = re.feed([0xAA, lo, hi, ...inner]); + expect(frames.length, 2); expect(frames.first.truncated, isTrue); - expect(frames.last.packetType, PacketType.metadata); + expect(frames.first.inner, isEmpty); + expect(frames.last.valid, isTrue, + reason: 'the real frame must not be swallowed by the phantom span'); expect(re.truncations, 1); });