Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/src/framing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions test/framing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);
});

Expand Down
Loading