mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* refactor(parsers): report format failures through one ParseFailure algebra Add ParseFailure (malformed, truncated, unsupported, limit, with format, offset and field) and its one carrier, ParseFailureError, in the domain. BinaryReader now names truncation and the field it read, and its native format and resource failures are ParseFailureError subclasses. Providers projected a format failure with their own instanceof ladder, and some dropped the selected path from the invalid_format message. parseFailureAnalysisError now owns that projection for the native, Go, devicetree, V8, Windows Shortcut and Electron fuse providers. The V8 code cache, Windows Shortcut and Electron fuse format classes are removed in favour of the algebra. The architecture guard counts raw Buffer and DataView reads, plain Error throws in parser folders and plain JSON.parse of parser input against baselines that can only shrink. * fix(parsers): stop reading every RangeError as a format failure Mach-O, Swift and Objective-C metadata parsers threw RangeError for a truncated or malformed record, so their catch sites treated any RangeError as a partial decode: a call stack overflow or a host string limit became an "unresolved" metadata reason, and the IL2CPP Mach-O path reported it as a malformed container. Those parsers and the CIL method body reader now throw ParseFailureError. hostRangeErrorKind is the one interpreter of a host RangeError (stack overflow, host capacity, raw out-of-bounds read), and parseFailureFromCause admits only a parse failure or a raw out-of-bounds read as evidence about the input. Fixup stream catch-alls rethrow host limits. The architecture guard counts the remaining direct instanceof RangeError checks. * fix(parsers): preserve Apple metadata failure ownership * test(architecture): shrink parser failure baseline * fix: preserve uncertainty for host range errors
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { inflateRawSync, deflateRawSync } from "node:zlib";
|
|
|
|
import { expect, it } from "vitest";
|
|
|
|
import {
|
|
hostRangeErrorKind,
|
|
isHostLimitFailure,
|
|
malformedInput,
|
|
parseFailureFromCause,
|
|
} from "./parseFailure.js";
|
|
|
|
const thrown = (run: () => unknown): unknown => {
|
|
try {
|
|
run();
|
|
} catch (cause: unknown) {
|
|
return cause;
|
|
}
|
|
throw new Error("expected the host to throw");
|
|
};
|
|
|
|
const recurse = (depth: number): number => recurse(depth + 1) + 1;
|
|
|
|
const stackOverflow = thrown(() => recurse(0));
|
|
const bufferPastEnd = thrown(() => Buffer.alloc(4).readUInt32LE(2));
|
|
const dataViewPastEnd = thrown(() =>
|
|
new DataView(new ArrayBuffer(2)).getUint32(0),
|
|
);
|
|
const inflatePastBound = thrown(() =>
|
|
inflateRawSync(deflateRawSync(Buffer.alloc(4096)), { maxOutputLength: 16 }),
|
|
);
|
|
|
|
it.each([
|
|
["a call stack overflow", stackOverflow, "stack-overflow"],
|
|
["an output past its allocation bound", inflatePastBound, "host-capacity"],
|
|
[
|
|
"an over-long string",
|
|
new RangeError("Invalid string length"),
|
|
"host-capacity",
|
|
],
|
|
["a Buffer read past its end", bufferPastEnd, undefined],
|
|
["a DataView read past its end", dataViewPastEnd, undefined],
|
|
["an invalid allocation", thrown(() => Buffer.alloc(-1)), undefined],
|
|
[
|
|
"an invalid write value",
|
|
thrown(() => Buffer.alloc(1).writeUInt8(256)),
|
|
undefined,
|
|
],
|
|
[
|
|
"a write past its end",
|
|
thrown(() => Buffer.alloc(4).writeUInt32LE(1, 2)),
|
|
undefined,
|
|
],
|
|
[
|
|
"a RangeError REA threw itself",
|
|
new RangeError("Truncated header"),
|
|
undefined,
|
|
],
|
|
["an unrelated error", new TypeError("not a range"), undefined],
|
|
] as const)("classifies %s", (_label, cause, kind) => {
|
|
expect(hostRangeErrorKind(cause)).toBe(kind);
|
|
expect(parseFailureFromCause(cause)).toBeUndefined();
|
|
expect(isHostLimitFailure(cause)).toBe(
|
|
kind === "stack-overflow" || kind === "host-capacity",
|
|
);
|
|
});
|
|
|
|
it("preserves an explicitly reported format failure", () => {
|
|
const reported = malformedInput("macho", "load command leaves the header");
|
|
expect(parseFailureFromCause(reported)).toBe(reported);
|
|
});
|
|
|
|
it.each([stackOverflow, inflatePastBound, new RangeError("Truncated header")])(
|
|
"leaves a host failure or an unclassified RangeError to the caller (%s)",
|
|
(cause) => {
|
|
expect(parseFailureFromCause(cause)).toBeUndefined();
|
|
},
|
|
);
|