fix(flutter): find snapshot headers that a read chunk boundary splits (#2804)
* fix(flutter): find snapshot headers that a read chunk boundary splits Within one chunk the snapshot hash scanner resumes four bytes after each magic, but across chunks it resumed after the whole 52-byte record and dropped the carried bytes. A magic starting 49-51 bytes after a rejected (non-hex) record was therefore lost when its four bytes straddled a read boundary, so the result depended on chunking and a conflicting hash could be omitted while coverage stayed complete. Resume after the magic in both cases. * refactor(flutter): drop the snapshot scanner cursor the fix superseded After a complete record the carried tail is shorter than a magic, and a pending magic has not been processed, so every push can rescan its carried bytes from the start without counting a record twice. Remove the absolute cursor and base offset, and cover sections 49-51 bytes after a rejected record with two-chunk and byte-at-a-time feeds.
This commit is contained in:
@@ -70,6 +70,37 @@ describe("SnapshotHashScanner", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([49, 50, 51])(
|
||||
"finds a section %i bytes after a rejected record at any chunking",
|
||||
(start) => {
|
||||
// A stray magic whose hash field is not hex, then a real section that
|
||||
// starts inside the stray record's magic-to-hash span.
|
||||
const payload = new Uint8Array(start + 52);
|
||||
payload.set([0xf5, 0xf5, 0xdc, 0xdc], 0);
|
||||
payload.set(
|
||||
realSnapshotSection("65817c30a78bb44c3dc3771876b6010a"),
|
||||
start,
|
||||
);
|
||||
const whole = new SnapshotHashScanner();
|
||||
whole.push(payload);
|
||||
expect(whole.result()).toEqual({
|
||||
capped: false,
|
||||
sources: 1,
|
||||
candidates: ["65817c30a78bb44c3dc3771876b6010a"],
|
||||
});
|
||||
for (let split = 1; split < payload.length; split += 1) {
|
||||
const scanner = new SnapshotHashScanner();
|
||||
scanner.push(payload.subarray(0, split));
|
||||
scanner.push(payload.subarray(split));
|
||||
expect(scanner.result(), `split at ${split}`).toEqual(whole.result());
|
||||
}
|
||||
const bytewise = new SnapshotHashScanner();
|
||||
for (let offset = 0; offset < payload.length; offset += 1)
|
||||
bytewise.push(payload.subarray(offset, offset + 1));
|
||||
expect(bytewise.result()).toEqual(whole.result());
|
||||
},
|
||||
);
|
||||
|
||||
it("ignores a magic without a following hash", () => {
|
||||
const scanner = new SnapshotHashScanner();
|
||||
scanner.push(Uint8Array.from([0xf5, 0xf5, 0xdc, 0xdc, 0xff, 0xff, 0xff]));
|
||||
|
||||
@@ -58,17 +58,16 @@ export class SnapshotHashScanner {
|
||||
private readonly candidates: string[] = [];
|
||||
private readonly candidateSet = new Set<string>();
|
||||
#buffer = new Uint8Array(0);
|
||||
/** Absolute stream offset from which unprocessed bytes begin. */
|
||||
#base = 0;
|
||||
#absoluteScanFrom = 0;
|
||||
|
||||
push(chunk: Uint8Array): void {
|
||||
const joined = new Uint8Array(this.#buffer.length + chunk.length);
|
||||
joined.set(this.#buffer, 0);
|
||||
joined.set(chunk, this.#buffer.length);
|
||||
// Resume after the last fully processed hash so carried bytes are not
|
||||
// counted twice across chunk boundaries.
|
||||
let offset = Math.max(0, this.#absoluteScanFrom - this.#base);
|
||||
// Carried bytes are an unprocessed pending magic or too short to hold a
|
||||
// magic, so rescanning them never counts a record twice. Within a chunk
|
||||
// the scan resumes after each magic, so a magic inside a previous
|
||||
// record's span is found wherever the chunk boundaries fall.
|
||||
let offset = 0;
|
||||
let pendingMagic: number | null = null;
|
||||
for (;;) {
|
||||
const magic = findBytes(joined, DART_SNAPSHOT_MAGIC, offset);
|
||||
@@ -96,21 +95,14 @@ export class SnapshotHashScanner {
|
||||
// magic record whose field is not lowercase hex is not a source.
|
||||
if (isValidHash) this.#sources += 1;
|
||||
offset = magic + DART_SNAPSHOT_MAGIC.length;
|
||||
this.#absoluteScanFrom =
|
||||
this.#base + magic + SNAPSHOT_HASH_OFFSET + SNAPSHOT_HASH_LENGTH;
|
||||
}
|
||||
// Keep a pending magic for the next chunk; otherwise keep only the
|
||||
// trailing bytes where a magic could still begin, so magic-free
|
||||
// streams never accumulate.
|
||||
const keepFrom =
|
||||
pendingMagic ??
|
||||
Math.max(
|
||||
this.#absoluteScanFrom - this.#base,
|
||||
joined.length - (DART_SNAPSHOT_MAGIC.length - 1),
|
||||
);
|
||||
Math.max(0, joined.length - (DART_SNAPSHOT_MAGIC.length - 1));
|
||||
this.#buffer = joined.slice(keepFrom);
|
||||
this.#base += keepFrom;
|
||||
if (pendingMagic !== null) this.#absoluteScanFrom = this.#base;
|
||||
}
|
||||
|
||||
result(): SnapshotHashScan {
|
||||
|
||||
Reference in New Issue
Block a user